Svcc2009 Async Ws


Published on

Asynchronous Web Services

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • For the BPEL entry we should not: <10’s seconds synchronous Asynchronous services >minutes then use BPEL
  • Note that this style is become more populate in web browser, for example the built in database in HTML 5 uses this form; but it can be harder to write code that works in this way because you need to make sure the main focus of the tool you are working on is ready.
  • But how do we relate message instances……
  • Can’t use AddressingFeature as once you have created any headers it wont work anymore – really annoying. Hence this longer code above. Could be using handlers but they are more painful when you need to set and extract instance information. Easier to put the code in-line
  • Annoyed this fix go pulled from Farralon as pyoung didn’t think it was important. I guess we just skirt the issue, or offer a dirty fat patch.
  • I supose the point to be made here is that The Async Reponder needs to be managed in some way….. Hence we are using JMS…..
  • Svcc2009 Async Ws

    1. 1. Asynchronous Web Services Pyounguk Cho, Oracle Corporation Manoj Kumar, Oracle Corporation
    2. 2. Agenda <ul><li>Introduction </li></ul><ul><li>Client side asynchrony </li></ul><ul><li>Server side asynchrony </li></ul><ul><li>Implementation of clients and services </li></ul><ul><li>Declarative asynchronous services </li></ul><ul><li>Advanced Topics </li></ul><ul><li>Q & A </li></ul>
    3. 3. Introduction Web Service <ul><li>a software system designed to support interoperable machine-to-machine interaction over a network </li></ul><ul><li>Using JAX-WS based tools.. </li></ul><ul><ul><li>Publish a POJO/SLSB annotated with javax.jws.WebService </li></ul></ul><ul><ul><li>WSDL gets created </li></ul></ul><ul><ul><li>Clients generate STUB using this WSDL </li></ul></ul><ul><ul><li>Make calls to WS using interface on stub </li></ul></ul>
    4. 4. Introduction Web Service <ul><li>package stockquote; </li></ul><ul><li>import javax.jws.WebService; </li></ul><ul><li>@WebService </li></ul><ul><li>public class StockQuote { </li></ul><ul><li>public float getPrice(String ticker) { </li></ul><ul><li>return 30; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    5. 5. Introduction WSDL <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li>< definitions xmlns:soap =&quot; &quot; xmlns:tns =&quot; http://stockquote/ &quot; xmlns:xsd =&quot; &quot; xmlns =&quot; &quot; targetNamespace =&quot; http://stockquote/ &quot; name =&quot; StockQuoteService &quot;> </li></ul>
    6. 6. WSDL Types <ul><li>< types > </li></ul><ul><li>< xsd:schema > </li></ul><ul><li>< xsd:import namespace =&quot; http://stockquote/ &quot; schemaLocation =&quot; http://localhost:7101/StockQuote-StockQuote-context-root/StockQuotePort?xsd=1 &quot;/> </li></ul><ul><li></ xsd:schema > </li></ul><ul><li></ types > </li></ul>
    7. 7. WSDL Messages <ul><li>< message name =&quot; getPrice &quot;> </li></ul><ul><li>< part name =&quot; parameters “ </li></ul><ul><li>element =&quot; tns:getPrice &quot;/> </li></ul><ul><li></ message > </li></ul><ul><li>< message name =&quot; getPriceResponse &quot;> </li></ul><ul><li>< part name =&quot; parameters “ </li></ul><ul><li>element =&quot; tns:getPriceResponse &quot;/> </li></ul><ul><li></ message > </li></ul>
    8. 8. WSDL Port Type <ul><li>< portType name =&quot; StockQuote &quot;> </li></ul><ul><li>< operation name =&quot; getPrice &quot;> </li></ul><ul><li> < input message =&quot; tns:getPrice &quot;/> </li></ul><ul><li> < output message =&quot; tns:getPriceResponse &quot;/> </li></ul><ul><li></ operation > </li></ul><ul><li></ portType > </li></ul>
    9. 9. WSDL Binding <ul><li>< binding name =&quot; StockQuotePortBinding “ </li></ul><ul><li>type =&quot; tns:StockQuote &quot;> </li></ul><ul><li>< soap:binding transport =&quot; &quot; style =&quot; document &quot;/> </li></ul><ul><li>< operation name =&quot; getPrice &quot;> </li></ul><ul><li></ operation > </li></ul><ul><li></ binding > </li></ul>
    10. 10. WSDL Service <ul><li>< service name =&quot; StockQuoteService &quot;> </li></ul><ul><li>< port name =&quot; StockQuotePort “ </li></ul><ul><li>binding =&quot; tns:StockQuotePortBinding &quot;> </li></ul><ul><li> < soap:address location =&quot; http://localhost:7101/StockQuote-StockQuote-context-root/StockQuotePort &quot;/> </li></ul><ul><li></ port > </li></ul><ul><li></ service > </li></ul><ul><li></ definitions > </li></ul>
    11. 11. Web Service API <ul><li>JAX-RPC </li></ul><ul><ul><li>Java API for XML-based RPC </li></ul></ul><ul><li>JAX-WS </li></ul><ul><ul><li>JAVA API for XML Web Services </li></ul></ul><ul><li>Invoke a method on a stub </li></ul><ul><li>Call gets converted to SOAP message </li></ul><ul><li>Soap Message is sent using http transport </li></ul>
    12. 12. Introduction Synchronous Call <ul><li>StockQuoteService service = new ...; </li></ul><ul><li>StockQuote stockQuote = service.getStockQuotePort(); </li></ul><ul><li>float price = stockQuote.getPrice(&quot;ORCL&quot;); </li></ul>
    13. 13. Introduction Asynchronous Call <ul><li>StockQuoteService service = new ...; </li></ul><ul><li>StockQuote stockQuote = service.getStockQuotePort(); </li></ul><ul><li>quoteService.getPriceAsync(“ORCL”); </li></ul><ul><li>// Do some other work </li></ul><ul><li>// How and when do we get the response? </li></ul>
    14. 14. Introduction Why Asynchronous? <ul><li>The world is fundamentally asynchronous </li></ul><ul><li>Improvement in .. </li></ul><ul><ul><li>User’s experience </li></ul></ul><ul><ul><li>Reliability in distributed applications </li></ul></ul><ul><ul><li>Scalability </li></ul></ul><ul><li>Only way to go in long running processes </li></ul><ul><li>Implementation </li></ul><ul><ul><li>Client side asynchrony </li></ul></ul><ul><ul><li>Server side asynchrony </li></ul></ul>
    15. 15. Introduction Alternatives to JAX-* <ul><li>BPEL </li></ul><ul><ul><li>Integration of other operations </li></ul></ul><ul><ul><li>Can be re-exposed as an asynchronous web service </li></ul></ul><ul><li>Asynchronous 3.1 EJB TM architecture </li></ul><ul><ul><li>Java only </li></ul></ul><ul><li>JMS, MQueue, Native Database Queues </li></ul><ul><ul><li>Generally platform specific </li></ul></ul><ul><li>SMTP </li></ul><ul><ul><li>Golden oldie </li></ul></ul><ul><li>SCA </li></ul>
    16. 16. Agenda <ul><li>Introduction </li></ul><ul><li>Client side asynchrony </li></ul><ul><li>Server side asynchrony </li></ul><ul><li>Implementation of clients and services </li></ul><ul><li>Declarative asynchronous services </li></ul><ul><li>Advanced Topics </li></ul><ul><li>Q & A </li></ul>
    17. 17. Client Side Asynchrony JAXWS 2.x API <ul><li>Calling synchronous services in asynchronous way </li></ul><ul><li>Not really synchronous though </li></ul><ul><li>WSDL to Java customization option </li></ul><ul><ul><li><jaxws: enableAsyncMapping >true</jaxws: ena… > </li></ul></ul><ul><li>Asynchronous Interface </li></ul><ul><ul><li>Polling </li></ul></ul><ul><ul><li>Callback </li></ul></ul><ul><li>Control of executors to manage the number of threads used </li></ul>
    18. 18. Client Side Asynchrony Synchronous Interface <ul><li>@WebService </li></ul><ul><li>public interface StockQuote { </li></ul><ul><li>float getPrice(String ticker); </li></ul><ul><li>} </li></ul>
    19. 19. Client Side Asynchrony Asynchronous Interface <ul><li>@WebService </li></ul><ul><li>public interface StockQuote { </li></ul><ul><li>float getPrice(String ticker); </li></ul><ul><li>Response<Float> </li></ul><ul><li>getPriceAsync(String ticker); </li></ul><ul><li>} </li></ul>
    20. 20. Client Side Asynchrony Polling Style <ul><li>StockQuote quoteService = (StockQuote)service.getPort(portName); </li></ul><ul><li>Response<Float> response = quoteService.getPriceAsync(ticker); </li></ul><ul><li>// do something else while the </li></ul><ul><li>// stock quote is in flight </li></ul><ul><li>float quote = response.get(); </li></ul>
    21. 21. Client Side Asynchrony Asynchronous Interface <ul><li>@WebService </li></ul><ul><li>public interface StockQuote { </li></ul><ul><li>float getPrice(String ticker); </li></ul><ul><li>Response<Float> getPriceAsync(String ticker); </li></ul><ul><li>Future<?> getPriceAsync(String ticker, AsyncHandler<Float> </li></ul><ul><li>responseReceiver); </li></ul><ul><li>} </li></ul>
    22. 22. Client Side Asynchrony Callback Style <ul><li>PriceReceiver priceReceiver = new PriceReceiver(); </li></ul><ul><li>quoteService.getPriceAsync(ticker, priceReceiver ); </li></ul><ul><li>.. </li></ul><ul><li>class PriceReceiver implements AsyncHandler<Float> { </li></ul><ul><li>public void handleResponse(Response<Float> response) { </li></ul><ul><li>Float price = response.get(); </li></ul><ul><li>// do something with the result </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    23. 23. Client Side Asynchrony Summary <ul><li>Call synchronous service in asynchronous way </li></ul><ul><li>Invoke async operation and </li></ul><ul><ul><li>Poll for the result </li></ul></ul><ul><ul><li>Provide a callback class </li></ul></ul><ul><ul><ul><li>Use your tricks to coordinate </li></ul></ul></ul><ul><li>Http time out still a problem </li></ul><ul><li>But works with “Any” synchronous service </li></ul>
    24. 24. Agenda <ul><li>Introduction </li></ul><ul><li>Client side asynchrony </li></ul><ul><li>Server side asynchrony </li></ul><ul><li>Implementation of clients and services </li></ul><ul><li>Declarative asynchronous services </li></ul><ul><li>Advanced Topics </li></ul><ul><li>Q & A </li></ul>
    25. 25. Server Side Asynchrony
    26. 26. Server Side Asynchrony <ul><li>Invocation and response separated </li></ul><ul><li>A new connection for the call and the response </li></ul><ul><li>The invoker and the receiver don’t have to be on the same machine </li></ul><ul><li>Invoker must pass the replyTo address </li></ul><ul><li>A message Id must flow for correlation </li></ul>
    27. 27. Server Side Asynchrony HTTP <ul><li>Generally has two port types, two binding; but one service </li></ul><ul><li>Can use partnerLinkType to relate the two different ports </li></ul><ul><li>Part of the BPEL specification; but an extension to WSDL </li></ul>
    28. 28. Server Side Asynchrony WS-Addressing <ul><li>Transport agnostic SOAP message delivery </li></ul><ul><li>Endpoints references and message information headers </li></ul><ul><li>Message information headers </li></ul><ul><ul><li>Allow you to relate the message to a particular instance </li></ul></ul><ul><li>Endpoint references </li></ul><ul><ul><li>Tell you where to send the message along with relevant metadata </li></ul></ul><ul><li>They can tell you where to reply to and send faults to, important for asynchronous services </li></ul>
    29. 29. Server Side Asynchrony Request <soap:Envelope> <soap:Header> <wsa:MessageID>uuid:35f19ca8-c9fe</wsa:MessageID> <wsa:Action>http://lh:80/request/…</wsa:Action> <wsa:ReplyTo> <wsa:Address>http://lh:77/response</wsa:Address> <wsa:ReferenceParameters> <customHeader>correlationKey</customHeader> </wsa:ReferenceParameters> </wsa:ReplyTo> <wsa:To>http://lh:80/request</wsa:To> </soap:Header> <soap:Body>…</soap:Body> </soap:Envelope>
    30. 30. Server Side Asynchrony Response <soap:Envelope> <soap:Header> <wsa:To>http://lh:77/response</wsa:To> <wsa:Action>urn:response</wsa:Action> <wsa:MessageID>uuid:cb383139-cdf2</wsa:MessageID> <wsa:RelatesTo>uuid:35f19ca8-c9fe</wsa:RelatesTo> <customHeader wsa:IsReferenceParameter=“1”> correlationKey</customHeader> </soap:Header> <soap:Body>…</soap:Body> </soap:Envelope>
    31. 31. Agenda <ul><li>Introduction </li></ul><ul><li>Client side asynchrony </li></ul><ul><li>Server side asynchrony </li></ul><ul><li>Implementation of clients and services </li></ul><ul><li>Declarative asynchronous services </li></ul><ul><li>Advanced Topics </li></ul><ul><li>Q & A </li></ul>
    32. 32. Server Side Asynchrony Client Implementation <ul><li>Create a proxy for the service </li></ul><ul><li>Implement the callback binding to receive the response </li></ul><ul><li>Deploy the callback web service </li></ul><ul><li>Instantiate a proxy for the initiation endpoint. </li></ul><ul><li>Set the MessageId property to a new unique value </li></ul><ul><li>Set the To ; ReplyTo ; and FaultTo URI </li></ul><ul><li>Invoke the web service </li></ul>
    33. 33. Server Side Asynchrony Client Implementation (contd) <ul><li>In the callback web service use the “ RelatesTo ” header or reference parameters to correlate the response </li></ul>
    34. 34. Server Side Asynchrony Client side invocation StockQuote sq = service.getStockQuote(); // Populate headers AddressingVersion av = AddressingVersion.W3C; WSBindingProvider wsbp = (WSBindingProvider)sq; WSEndpointReference replyTo = new WSEndpointReference( “ http://lh:77/response ” , av ); String uuid = “uuid:” + UUID.randomUUID(); wsbp.setOutboundHeaders( new StringHeader(av.messageIDTag, uuid), new StringHeader(av.toTag,”http://lh:88/…”), replyTo.createHeader(av.replyToTag); sq.getPrice(“ORCL”);
    35. 35. Server Side Asynchrony Callback Implementation // Deal with response @Resource WebServiceContext wsContext; public void getPriceResponse(String _return) { HeaderList hl = wsContext.getMessageContext().get( JAXWSPRopertiers.INBOUND_HEADER_LIST_PROPERTY); Header h = hl.get(AddressingVersion.W3C.relatesToTag()); String relatesToMessageId = h.getStringContents(); … } // How about receiving a fault?
    36. 36. Server Side Asynchrony FaultTo in Callback <ul><li>Just create a @WebServiceProvider? </li></ul><ul><li>There was a bug in JAX-WS RI (Issue 585, fixed in 2.1.5) </li></ul><ul><li>You might end up having to use a Servlet if on an older version. </li></ul><ul><li>JAX-WS should support it in future </li></ul>
    37. 37. Server Side Asynchrony Service Implementation <ul><li>Accept the asynchronous request and save it (where?) </li></ul><ul><li>Return 202 Accepted </li></ul><ul><li>Pick the request and process it </li></ul><ul><li>Invoke the callback service and pass the result </li></ul>
    38. 38. Server Side Asynchrony Using JMS Queue for incoming request
    39. 39. Server Side Asynchrony Using response queue
    40. 40. Agenda <ul><li>Introduction </li></ul><ul><li>Client side asynchrony </li></ul><ul><li>Server side asynchrony </li></ul><ul><li>Implementation of clients and services </li></ul><ul><li>Declarative asynchronous services </li></ul><ul><li>Advanced Topics </li></ul><ul><li>Q & A </li></ul>
    41. 41. Declarative Asynchronous Services <ul><li>Implementing is not trivial </li></ul><ul><li>Why not use a declarative model? </li></ul><ul><li>Coming in Fusion Middleware, based on the JMS model </li></ul><ul><li>Just add one annotation in POJO/SLSB! </li></ul>
    42. 42. Declarative Asynchronous Services package com.stock; import …; @ Portable WebService @ AsyncWebService public class StockQuote { public float getPrice(String ticker) { return 100; } } Yes Just One Annotation
    43. 43. Declarative Asynchronous Services <ul><li>The resultant WSDL </li></ul>
    44. 44. Agenda <ul><li>Introduction </li></ul><ul><li>Client side asynchrony </li></ul><ul><li>Server side asynchrony </li></ul><ul><li>Implementation of clients and services </li></ul><ul><li>Declarative asynchronous services </li></ul><ul><li>Advanced Topics </li></ul><ul><li>Q & A </li></ul>
    45. 45. Advanced Topics Policy for Asynchronous Services <ul><li>Asynchronous Service </li></ul><ul><ul><li>Server side policy for incoming request (2) </li></ul></ul><ul><ul><li>Callback client side policy to send response (3) </li></ul></ul><ul><li>Asynchronous Client </li></ul><ul><ul><li>Client side policy to invoke asynchronous operation (1) </li></ul></ul><ul><ul><li>Callback server side policy to receive response (4) </li></ul></ul>
    46. 46. Advanced Topics Policy for Asynchronous Services <ul><li>Multiple ad hoc clients for service </li></ul><ul><ul><li>Use PKI for web of trust, servers trust servers </li></ul></ul><ul><ul><ul><li>Store incoming client public key for encrypting response where available </li></ul></ul></ul><ul><ul><ul><li>Or pass keys using WS-Addressing headers and WS-Identity </li></ul></ul></ul><ul><ul><li>Use SAML, callback reuses identity of invoker </li></ul></ul><ul><ul><li>M:N configuration, nightmare </li></ul></ul>
    47. 47. Advanced Topics Transaction Control
    48. 48. Advanced Topics Reliability <ul><li>Make piece wise transactional and reliable </li></ul><ul><li>Ensure that request and response do not get lost </li></ul><ul><li>Make MDBs run in transaction </li></ul><ul><ul><li>Container managed transaction demarcation </li></ul></ul><ul><ul><li>Bean managed transaction demarcation </li></ul></ul><ul><li>Setup JMS queue for retries </li></ul><ul><li>Setup error queues for request and response queues </li></ul>
    49. 49. Summary <ul><li>Client side asynchrony of JAX-WS </li></ul><ul><ul><li>Works for any synchronous services </li></ul></ul><ul><ul><li>Polling or callback style API </li></ul></ul><ul><li>Server side asynchrony </li></ul><ul><ul><li>Two separate one way calls for request and response </li></ul></ul><ul><ul><li>Use WS-Addressing for message correlation </li></ul></ul><ul><ul><li>Client Side </li></ul></ul><ul><ul><ul><li>Pass Message Id and ReplyTo address </li></ul></ul></ul><ul><ul><ul><li>Implement Callback service, and receive relatesToMessageId </li></ul></ul></ul><ul><ul><li>Server side </li></ul></ul><ul><ul><ul><li>Save the request in a queue, return asap </li></ul></ul></ul><ul><ul><ul><li>Process the request </li></ul></ul></ul><ul><ul><ul><li>Call callback service with the response </li></ul></ul></ul><ul><ul><li>Transaction and reliability </li></ul></ul>
    50. 50. Q & A <ul><li>See Also </li></ul><ul><ul><li>http:// jax - ws </li></ul></ul><ul><ul><li> jdev / </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul><ul><li>Personal blogs </li></ul><ul><ul><li>http:// kingsfleet . blogspot .com </li></ul></ul><ul><ul><li>http://manoj- kumar -on. blogspot .com </li></ul></ul>