Asynchronous Webservice

Asynchronous Webservice Client: Client side asynchrony can be achieved using the following,



  1. Async Polling

  2. Async Callback-handler


1. Create a Calculator Application using the examples which comes with glassfishesb/netbeans


 calculator app


2.  Observe the webservice client for the above, note the webservice interface has a single operation which is by default a synchronous call, we will see how to generate the interface which makes the same call in an asynchronous way



3. Enable Asynchronous Client : note the interface now has two more operations


enable asyn client


4. How to invoke using the above generated service endpoint interface ,


Async Polling


The client application will invoke the async polling operation on the stub and check for a response on the Response object. The response is available when Response.isDone returns true.



javax.xml.ws.Response<com.sun.CalculateResponse> resp = port.calculateAsync(x, y);
 while(!resp.isDone()) {
  // do something


}
System.out.println("Result = "+resp.get().getReturn());
 



Async CallbackHandler


In this case the  client application has to provide an AsyncHandler by implementing the javax.xml.ws.AsyncHandler<T> interface.



// Async callback handler


class CalculatorWSCallbackHandler implements javax.xml.ws.AsyncHandler<com.sun.CalculateResponse> {
    private com.sun.CalculateResponse output;


    public void handleResponse(javax.xml.ws.Response<com.sun.CalculateResponse> response) {
        try {
            output = response.get();
        } catch(Exception ex) {
            // handle exception
        }
    }


    com.sun.CalculateResponse getResponse() {
         return output;
    }
}


// The async handler is then passed as a parameter of the async callback method:


CalculatorWSCallbackHandler asyncHandler = new CalculatorWSCallbackHandler();
 // process asynchronous response here
 java.util.concurrent.Future<? extends java.lang.Object> result = port.calculateAsync(x, y, asyncHandler);
 while(!result.isDone()) {
  // do something
 }
 out.println("Result = "+asyncHandler.getResponse().getReturn());
  



5. Using the Dispatch Api


Four types of MEP are supported using the dispatch API



  • Object response = dispatch.invoke(T);

  • dispatch.invokeOneway(T);

  • Response<T> response = dispatch.invokeAsync(T);

  • Future<?> response = dispatch.invokeAsync(T, AsyncHandler);



The example shows how the asynchronous invocation can be made while using the SOAPMessage

Aysnc Polling



Response<SOAPMessage> res = dispatch.invokeAsync(soapMessage);
 while (!res.isDone()) {
     //do some work
 }
 SOAPMessage soapM = res.get();



Async CallbackHandler



class ResponseHandler implements javax.xml.ws.AsyncHandler<SOAPMessage> {



 SOAPMessage output = null;


 public void handleResponse(Response<SOAPMessage> arg0) {
     try {
  output = arg0.get();
     } catch (Exception e) {
  // handle exception
     }
 }
 SOAPMessage getResponse() {
     return output;
 }
      }
 



 ResponseHandler rhandler = new ResponseHandler();
 Future<? extends java.lang.Object> f = dispatch.invokeAsync(message, rhandler);
 while(!f.isDone()){
     //do some work
 }
 
 SOAPMessage m = rhandler.getResponse();



Using WS-Addressing


WS-addressing supports asynchronous MEP , here is one example taken from the specification, Note the replyTo part of the Request message, Any service implementing ws-addressing specification will send the response message based on the replyTo Message addresing property



Request 



<S:Envelope
  <wsa:MessageID>abc</wsa:MessageID>
    <wsa:ReplyTo>
    <wsa:Address>http://abc.com/client1</wsa:Address>
    </wsa:ReplyTo>
    <wsa:To S:mustUnderstand="1">http://fabrikam123.example</wsa:To>
    <wsa:Action>http://fabrikam123.example/mail/Delete</wsa:Action>
  </S:Header>
  <S:Body>
    <f123:Delete>    
       <maxCount>42</maxCount>
    </f123:Delete>
  </S:Body>
</S:Envelope>



Response



<S:Envelope
    <wsa:MessageID>xyz</wsa:MessageID>
    <wsa:RelatesTo>abc</wsa:RelatesTo>
    <wsa:To S:mustUnderstand="1">
     
http://abc.com/client1
    </wsa:To>
    <wsa:Action>http://fabrikam123.example/mail/DeleteAck</wsa:Action>
  </S:Header>
  <S:Body>
    <f123:DeleteAck/>
  </S:Body>
</S:Envelope>




 


The diagram below shows the flow  of messages


ws-addressing


Note: if replyTo contains an anonymous URI


http://schemas.xmlsoap.org/ws/2003/03/addressing/role/anonymous



When an application/client uses anonymous URI, it says that there is no real endpoint available for this address. Using the anonymous URI is equivalent to not using the ReplyTo header, the response must flow back in the HTTP response message. The web-service must be able to dynamically switch between synchronous and asynchronous modes on the basis of replyTo or faultTo headers.


 


More info



  1. Async Client

  2. WS-Addressing

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

sujit

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today