Building Asynchronous
Services with SCA

Mike Edwards
Raymond Feng
Luciano Resende
Agenda
>   Service Component Architecture
>   Why Asynchronous Services?
>   Facilities for Async Services
>   SCA support for service interactions
>   Example Async Service & Client
>   Events and Async Services compared
>   Demo Scenario
>   Summary
What SCA is
  >   executable model for assembling services
       ●   composites provide language to compose and
           configure service components
       ●   handles service dependencies
  >   Simplified component programming model for
      implementing services
       ●   BPEL processes, JavaTM POJOs, EJBs, COBOL,
           PHP scripts, C++ apps, JavaScript & AJAX, XSLT…
  >   Late binding of policy and communication
      methods, with distributed deployment model
SCA Overview
Service Interface                                                                           Reference Interface
 - Java                                                 Property                              - Java
 - WSDL                                                                                       - WSDL


              Composite A
                                          property setting



                              Component                            Component
         Service                  A                                    B                 Reference




                    promote                      wire                          promote




 Service Binding               Implementation                                            Reference Binding
  Web Service                    Java                                                     Web Service
  JMS                            BPEL                                                     JMS
  SLSB                           PHP                                                      SLSB
  Rest                           SCA composite                                            Rest
  JSONRPC                        spring                                                   JSONRPC
  JCA                            EJB module                                               JCA
  …                              …                                                        …
Why SCA makes life simpler
  >   One way to look at SCA is that it takes details of
       ●   access methods and endpoints
       ●   implementations and configuration
       ●   policy such as encryption, authentication
  >   …and moves them into middleware layer
  >   Application developers write business logic:
      code that actually builds value
       ●   details of using services handled by SCA
       ●   late binding: as details change, applications (and
           developers who wrote them) aren’t affected
       ●   "no plumbing in the code"
Why SCA makes life simpler
  >   SCA gives developers
      single programming model for using services
      for all aspects of service lifecycle:
      –   Construction
      –   Assembly
      –   Deployment


  >   As your SOA gets more complex, developers have to
      learn more and more interfaces
      ●   In JavaTM alone: EJBs, RMI, JCA, JAX-WS, JMS...
Service Component Architecture (SCA):
Simplified Programming Model for SOA
 >   model for:
 >   building service components
 >   assembling components into applications
 >   deploying to (distributed) runtime environments
       - Service components built from new or existing code
         using SOA principles
       - vendor-neutral – supported across the industry
       - language-neutral – components written using any
         language
       - technology-neutral – use any communication
         protocols and infrastructure to link components
Agenda
>   Service Component Architecture
>   Why Asynchronous Services?
>   Facilities for Async Services
>   SCA support for service interactions
>   Example Async Service & Client
>   Events and Async Services compared
>   Demo Scenario
>   Summary
Asynchronous interactions
  >   Typical in business processes
  >   …not everything happens immediately
  >   …a single request can result in a
      sequence of responses over time
  >   …a client probably does not want to wait
      while all steps are completed
Synchronous Processing
                            Price of IBM stock?



           IBM = $121.50


                            Price of AT&T stock?



            AT&T = $38.72
  Client                                           Provider
Asynchronous Processing
                     Order Books X, Y, Z



           Acknowledge, OrderID = 3824




           OrderID = 3824 Expect dispatch = 12/06/2008


  Client                                                    Provider
           OrderID = 3824 Books X,Y dispatched 10/06/2008
           Package ID = 478842110


           OrderID = 3824 Books Z dispatched 13/06/2008
           Package ID = 479567736
Why Asynchronous Services?
>   Business processes involve long running
    multi-step sequences
>   Clients & Services cannot afford to wait
    on long request/response operations
     ●   impact on processing resources,
         memory, communication resources
Agenda

>   Service Component Architecture
>   Why Asynchronous Services?
>   Facilities for Async Services
>   SCA support for service interactions
>   Example Async Service & Client
>   Events and Async Services compared
>   Demo Scenario
>   Summary
Synchronous Services
>   plenty of support for synchronous
    programming
>   call-and-return typical for most
    programming languages
         SearchResponse search(SearchRequest searchRequest);


>   most service frameworks support this -
    for clients & service providers
Asynchronous Services
>   Facilities less common
     ●   JavaTM concurrency classes
     ●   JAX-WS asynchronous client
     ●   JMS messaging
     ●   BPEL
JAX-WS Async Client API
    SearchResponse search(SearchRequest sRequest );




    Future<?> searchAsync(SearchRequest oRequest,
               AsyncHandler<SearchResponse> responseHandler );

                               +
class SearchResponseHandler
      implements AsyncHandler<SearchResponse> {

    public void handleResponse( Response<SearchResponse> theResponse )
    {
       SearchResponse orderResponse = theResponse.get();
    }
}
JMS/Messaging
>   Supports async clients and async services



          Client                                                    Service



     a.send( message1 )                                    message1 = x.receive( )
            .                                                       .
            .                                                       .
            .                                                       .

    message2 = b.receive( )                                  y.send( message2 )




                   + messageListener onMessage listener interface
BPEL and Async Processing
>   BPEL designed to handle:
     ●   long running processes
     ●   with asynchrony
     ●   multiple partner relationships
Agenda

>   Service Component Architecture
>   Why Asynchronous Services?
>   Facilities for Async Services
>   SCA support for service interactions
>   Example Async Service & Client
>   Events and Async Services compared
>   Demo Scenario
>   Summary
SCA Service Interaction

                                                          AccountData
   TripCataclog                                                 Flight
                                                          Service
   Component                                                 Component
                                                          Component




                              Callback (optional)

                  Reference                         Service



       Synchronous or Asynchronous interaction styles supported
       - depends on the interface definition
SCA Callbacks
>   SCA supports Async services using Callbacks
     ●   service has regular forward call interface
     ●   + callback interface on which service makes calls
         back to clients
          ●   clients implement the callback interface
●   SCA callbacks are part of the business
●   Forward calls and callbacks can use different
    styles such as:
     ●   Oneway + Req/Res
     ●   Req/Res + Req/Res
     ●   Req/Res + Oneway
     ●   Oneway + Oneway
Sync and Async Interfaces
public interface SyncSearchService {
    public SearchResponse search( SearchRequest sRequest );
}



public interface AsyncSearchService {
        public void search( SearchRequest sRequest );
}


public interface AsyncSearchCallback {
        public void searchResponse( SearchResponse sResponse );
}
Callback Interfaces
>   Callback interface
     ●   may have multiple operations/methods
     ●   operations may be called at any time
         after initial forward call to service
     ●   number and sequence of callback
         operations for given forward call is
         variable (0, 1, many…)
Bidirectional Service Interaction
•   The reference creates a callback endpoint to
    receive callbacks
•   The service establishes a “callback” reference to
    the caller
•   The forward call can use different binding than the
    callback
        Callback
        endpoint for
        reference               flightSearch
                       Search                               Flight

                                           binding



                                         Callback binding
Agenda

>   Service Component Architecture
>   Why Asynchronous Services?
>   Facilities for Async Services
>   SCA support for service interactions
>   Example Async Service & Client
>   Events and Async Services compared
>   Demo Scenario
>   Summary
Bidirectional Interface
•   Forward Interface
        @Remotable
        @Callback(SearchCallback.class)
        public interface Search {
            TripItem[] searchSynch(TripLeg tripLeg);
            @OneWay
            void searchAsynch(TripLeg tripLeg);
        }

•   Callback Interface
        @Remotable
        public interface SearchCallback {
            void searchResults(TripItem[] items);
        }
Trip Async Search
                                                 AccountData
                           Callback                  Flight
                                                 Service
                                                  Component
                                                 Component

       Trip
     Cataclog
    Component                         Callback
                                                   AccountData
                                                       Hotel
                                                   Service
                                                    Component
                                                   Component




                           Callback               AccountData
                                                       Car
                                                  Service
                                                   Component
                Callback                          Component
                             AccountData
                               Packages
                             Service
                              Component
                             Component
Flight Service implementation
import org.osoa.sca.annotations.*;
public class FlightImpl implements Search, Book {

    @Callback
    protected SearchCallback searchCallback;

    ……….

    public void searchAsynch(TripLeg tripLeg) {

        ……….

        // return available flights
        searchCallback.searchResults(searchSynch(tripLeg));
    }
}
Catalog client application
 import org.osoa.sca.annotations.*;
 public class TravelCatalogImpl implements TravelCatalogSearch, SearchCallback{

     @Reference
     protected Search flightSearch;
     ……

  public TripItem[] search(TripLeg tripLeg) {
     flightSearch.searchAsynch(tripLeg);
      ……
  }

  public void searchResults(TripItem[] items){
    if (items != null) {
         for(int i = 0; i < items.length; i++ ){
           searchResults.add(items[i]);
         }
     }
    ……
  }

 }
Composite
<component name="TravelCatalogComponent">
        <implementation.java class="scatours.travelcatalog.TravelCatalogImpl"/>
        <reference name="flightSearch">
            <interface.java interface="scatours.common.Search"
                            callbackInterface="scatours.common.SearchCallback"/>
            <binding.ws uri="http://localhost:8080/Flight/Search"/>
            <callback>
                <binding.ws uri="http://localhost:8080/Flight/SearchCallback"/>
            </callback>
        </reference>
</component>

<component name="FlightComponent">                                       forward interface
        <implementation.java class="scatours.flight.FlightImpl"/>
        <service name="Search">
            <interface.java interface="scatours.common.Search"
                            callbackInterface="scatours.common.SearchCallback"/>
            <binding.ws uri="http://localhost:8080/Flight/Search"/>
            <tuscany:binding.jsonrpc/>
            <callback>                                                  callback interface
                <binding.ws uri="http://localhost:8080/Flight/SearchCallback"/>
            </callback>
        </service>
        <service name="Book"/>
</component>                                                             callback binding
"under the hood"
>   How do the callbacks work in practice?
     ●   implemented using suitable
         communication methods, such as:

     ●   JMS messaging
     ●   Web services using WS-Addressing
Agenda

>   Service Component Architecture
>   Why Asynchronous Services?
>   Facilities for Async Services
>   SCA support for service interactions
>   Example Async Service & Client
>   Events and Async Services compared
>   Demo Scenario
>   Summary
Asynchrony & Events
>   Another way to handle asynchrony is to
    use events
>   components communicate by producing
    and consuming events
     ●   these are one-way
     ●   asychronous
     ●   arbitrary sequences

>   eg JMS messages
Events or Callbacks
>   Both allow asynchrony
>   Calls target single specific service
>   Callbacks ensure responses go back to
    original requester
>   Event handling is more flexible
     ●   pub/sub, broadcasting of events
     ●   no guarantee that anyone listens
Agenda

>   Service Component Architecture
>   Why Asynchronous Services?
>   Facilities for Async Services
>   SCA support for service interactions
>   Example Async Service & Client
>   Events and Async Services compared
>   Demo Scenario
>   Summary
SCA Tours
  Online Travel Sample Scenario



http://svn.apache.org/repos/asf/tuscany/sandbox/travelsample




                                                               36
SCA Tours – Asynchronous Search
Services
                                                 fullapp-packagedtrip (8085)


                                                               Trip
                                                               Partner




                                                             Java
          fullapp-frontend (8084)



             TravelCatalog                        fullapp-bespoketrip (8086)
                                    binding.ws
                                                              Hotel
                                                              Partner


                                                                 Java
             Java


                                                              Flight
                                                              Partner

                                                                 Java




                                                               Car
                                                               Partner

                                                                 Java
Agenda

>   Service Component Architecture
>   Why Asynchronous Services?
>   Facilities for Async Services
>   SCA support for service interactions
>   Example Async Service & Client
>   Events and Async Services compared
>   Demo Scenario
>   Summary
Summary
>   Asynchronous services are a fact of life
>   SCA makes providing async services and
    their clients simpler and easier
>   SCA supports this on a range of
    communication methods
Useful links
>   Articles about SCA:
       http://www.infoq.com/articles/setting-out-for-sca
       http://www.osoa.org/display/Main/SCA+Resources

>   Apache Tuscany - Open Source implementation of SCA:
       http://tuscany.apache.org/

>   SCA Specifications in OASIS:
       http://www.oasis-opencsa.org/
Mike Edwards
mike_edwards@uk.ibm.com
Raymond Feng
rfeng@us.ibm.com
Luciano Resende
luciano_resende@us.ibm.com

Building Asynchronous Services With Sca

  • 1.
    Building Asynchronous Services withSCA Mike Edwards Raymond Feng Luciano Resende
  • 2.
    Agenda > Service Component Architecture > Why Asynchronous Services? > Facilities for Async Services > SCA support for service interactions > Example Async Service & Client > Events and Async Services compared > Demo Scenario > Summary
  • 3.
    What SCA is > executable model for assembling services ● composites provide language to compose and configure service components ● handles service dependencies > Simplified component programming model for implementing services ● BPEL processes, JavaTM POJOs, EJBs, COBOL, PHP scripts, C++ apps, JavaScript & AJAX, XSLT… > Late binding of policy and communication methods, with distributed deployment model
  • 4.
    SCA Overview Service Interface Reference Interface - Java Property - Java - WSDL - WSDL Composite A property setting Component Component Service A B Reference promote wire promote Service Binding Implementation Reference Binding Web Service Java Web Service JMS BPEL JMS SLSB PHP SLSB Rest SCA composite Rest JSONRPC spring JSONRPC JCA EJB module JCA … … …
  • 5.
    Why SCA makeslife simpler > One way to look at SCA is that it takes details of ● access methods and endpoints ● implementations and configuration ● policy such as encryption, authentication > …and moves them into middleware layer > Application developers write business logic: code that actually builds value ● details of using services handled by SCA ● late binding: as details change, applications (and developers who wrote them) aren’t affected ● "no plumbing in the code"
  • 6.
    Why SCA makeslife simpler > SCA gives developers single programming model for using services for all aspects of service lifecycle: – Construction – Assembly – Deployment > As your SOA gets more complex, developers have to learn more and more interfaces ● In JavaTM alone: EJBs, RMI, JCA, JAX-WS, JMS...
  • 7.
    Service Component Architecture(SCA): Simplified Programming Model for SOA > model for: > building service components > assembling components into applications > deploying to (distributed) runtime environments - Service components built from new or existing code using SOA principles - vendor-neutral – supported across the industry - language-neutral – components written using any language - technology-neutral – use any communication protocols and infrastructure to link components
  • 8.
    Agenda > Service Component Architecture > Why Asynchronous Services? > Facilities for Async Services > SCA support for service interactions > Example Async Service & Client > Events and Async Services compared > Demo Scenario > Summary
  • 9.
    Asynchronous interactions > Typical in business processes > …not everything happens immediately > …a single request can result in a sequence of responses over time > …a client probably does not want to wait while all steps are completed
  • 10.
    Synchronous Processing Price of IBM stock? IBM = $121.50 Price of AT&T stock? AT&T = $38.72 Client Provider
  • 11.
    Asynchronous Processing Order Books X, Y, Z Acknowledge, OrderID = 3824 OrderID = 3824 Expect dispatch = 12/06/2008 Client Provider OrderID = 3824 Books X,Y dispatched 10/06/2008 Package ID = 478842110 OrderID = 3824 Books Z dispatched 13/06/2008 Package ID = 479567736
  • 12.
    Why Asynchronous Services? > Business processes involve long running multi-step sequences > Clients & Services cannot afford to wait on long request/response operations ● impact on processing resources, memory, communication resources
  • 13.
    Agenda > Service Component Architecture > Why Asynchronous Services? > Facilities for Async Services > SCA support for service interactions > Example Async Service & Client > Events and Async Services compared > Demo Scenario > Summary
  • 14.
    Synchronous Services > plenty of support for synchronous programming > call-and-return typical for most programming languages SearchResponse search(SearchRequest searchRequest); > most service frameworks support this - for clients & service providers
  • 15.
    Asynchronous Services > Facilities less common ● JavaTM concurrency classes ● JAX-WS asynchronous client ● JMS messaging ● BPEL
  • 16.
    JAX-WS Async ClientAPI SearchResponse search(SearchRequest sRequest ); Future<?> searchAsync(SearchRequest oRequest, AsyncHandler<SearchResponse> responseHandler ); + class SearchResponseHandler implements AsyncHandler<SearchResponse> { public void handleResponse( Response<SearchResponse> theResponse ) { SearchResponse orderResponse = theResponse.get(); } }
  • 17.
    JMS/Messaging > Supports async clients and async services Client Service a.send( message1 ) message1 = x.receive( ) . . . . . . message2 = b.receive( ) y.send( message2 ) + messageListener onMessage listener interface
  • 18.
    BPEL and AsyncProcessing > BPEL designed to handle: ● long running processes ● with asynchrony ● multiple partner relationships
  • 19.
    Agenda > Service Component Architecture > Why Asynchronous Services? > Facilities for Async Services > SCA support for service interactions > Example Async Service & Client > Events and Async Services compared > Demo Scenario > Summary
  • 20.
    SCA Service Interaction AccountData TripCataclog Flight Service Component Component Component Callback (optional) Reference Service Synchronous or Asynchronous interaction styles supported - depends on the interface definition
  • 21.
    SCA Callbacks > SCA supports Async services using Callbacks ● service has regular forward call interface ● + callback interface on which service makes calls back to clients ● clients implement the callback interface ● SCA callbacks are part of the business ● Forward calls and callbacks can use different styles such as: ● Oneway + Req/Res ● Req/Res + Req/Res ● Req/Res + Oneway ● Oneway + Oneway
  • 22.
    Sync and AsyncInterfaces public interface SyncSearchService { public SearchResponse search( SearchRequest sRequest ); } public interface AsyncSearchService { public void search( SearchRequest sRequest ); } public interface AsyncSearchCallback { public void searchResponse( SearchResponse sResponse ); }
  • 23.
    Callback Interfaces > Callback interface ● may have multiple operations/methods ● operations may be called at any time after initial forward call to service ● number and sequence of callback operations for given forward call is variable (0, 1, many…)
  • 24.
    Bidirectional Service Interaction • The reference creates a callback endpoint to receive callbacks • The service establishes a “callback” reference to the caller • The forward call can use different binding than the callback Callback endpoint for reference flightSearch Search Flight binding Callback binding
  • 25.
    Agenda > Service Component Architecture > Why Asynchronous Services? > Facilities for Async Services > SCA support for service interactions > Example Async Service & Client > Events and Async Services compared > Demo Scenario > Summary
  • 26.
    Bidirectional Interface • Forward Interface @Remotable @Callback(SearchCallback.class) public interface Search { TripItem[] searchSynch(TripLeg tripLeg); @OneWay void searchAsynch(TripLeg tripLeg); } • Callback Interface @Remotable public interface SearchCallback { void searchResults(TripItem[] items); }
  • 27.
    Trip Async Search AccountData Callback Flight Service Component Component Trip Cataclog Component Callback AccountData Hotel Service Component Component Callback AccountData Car Service Component Callback Component AccountData Packages Service Component Component
  • 28.
    Flight Service implementation importorg.osoa.sca.annotations.*; public class FlightImpl implements Search, Book { @Callback protected SearchCallback searchCallback; ………. public void searchAsynch(TripLeg tripLeg) { ………. // return available flights searchCallback.searchResults(searchSynch(tripLeg)); } }
  • 29.
    Catalog client application import org.osoa.sca.annotations.*; public class TravelCatalogImpl implements TravelCatalogSearch, SearchCallback{ @Reference protected Search flightSearch; …… public TripItem[] search(TripLeg tripLeg) { flightSearch.searchAsynch(tripLeg); …… } public void searchResults(TripItem[] items){ if (items != null) { for(int i = 0; i < items.length; i++ ){ searchResults.add(items[i]); } } …… } }
  • 30.
    Composite <component name="TravelCatalogComponent"> <implementation.java class="scatours.travelcatalog.TravelCatalogImpl"/> <reference name="flightSearch"> <interface.java interface="scatours.common.Search" callbackInterface="scatours.common.SearchCallback"/> <binding.ws uri="http://localhost:8080/Flight/Search"/> <callback> <binding.ws uri="http://localhost:8080/Flight/SearchCallback"/> </callback> </reference> </component> <component name="FlightComponent"> forward interface <implementation.java class="scatours.flight.FlightImpl"/> <service name="Search"> <interface.java interface="scatours.common.Search" callbackInterface="scatours.common.SearchCallback"/> <binding.ws uri="http://localhost:8080/Flight/Search"/> <tuscany:binding.jsonrpc/> <callback> callback interface <binding.ws uri="http://localhost:8080/Flight/SearchCallback"/> </callback> </service> <service name="Book"/> </component> callback binding
  • 31.
    "under the hood" > How do the callbacks work in practice? ● implemented using suitable communication methods, such as: ● JMS messaging ● Web services using WS-Addressing
  • 32.
    Agenda > Service Component Architecture > Why Asynchronous Services? > Facilities for Async Services > SCA support for service interactions > Example Async Service & Client > Events and Async Services compared > Demo Scenario > Summary
  • 33.
    Asynchrony & Events > Another way to handle asynchrony is to use events > components communicate by producing and consuming events ● these are one-way ● asychronous ● arbitrary sequences > eg JMS messages
  • 34.
    Events or Callbacks > Both allow asynchrony > Calls target single specific service > Callbacks ensure responses go back to original requester > Event handling is more flexible ● pub/sub, broadcasting of events ● no guarantee that anyone listens
  • 35.
    Agenda > Service Component Architecture > Why Asynchronous Services? > Facilities for Async Services > SCA support for service interactions > Example Async Service & Client > Events and Async Services compared > Demo Scenario > Summary
  • 36.
    SCA Tours Online Travel Sample Scenario http://svn.apache.org/repos/asf/tuscany/sandbox/travelsample 36
  • 37.
    SCA Tours –Asynchronous Search Services fullapp-packagedtrip (8085) Trip Partner Java fullapp-frontend (8084) TravelCatalog fullapp-bespoketrip (8086) binding.ws Hotel Partner Java Java Flight Partner Java Car Partner Java
  • 38.
    Agenda > Service Component Architecture > Why Asynchronous Services? > Facilities for Async Services > SCA support for service interactions > Example Async Service & Client > Events and Async Services compared > Demo Scenario > Summary
  • 39.
    Summary > Asynchronous services are a fact of life > SCA makes providing async services and their clients simpler and easier > SCA supports this on a range of communication methods
  • 40.
    Useful links > Articles about SCA: http://www.infoq.com/articles/setting-out-for-sca http://www.osoa.org/display/Main/SCA+Resources > Apache Tuscany - Open Source implementation of SCA: http://tuscany.apache.org/ > SCA Specifications in OASIS: http://www.oasis-opencsa.org/
  • 41.