blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

JAX-WS Asynchronous

No changes to be made to server code. It is the client that acts asynchronous.

I used a standalone WS client to test it.

System.out.println("Client calling WS");
CalculatorSOAPProxy proxy = new CalculatorSOAPProxy();
proxy._getDescriptor().setEndpoint(
        "http://localhost:9089/WS2Server/Calculator");
CalcRequestType in = new CalcRequestType();
in.setOperand1(5);
in.setOperand2(10);
in.setOperation(OperationType.ADD);
/**
 * Using a polling model
 */

Response response = proxy.calculateAsync(in);
System.out.println("Checking result");

while (!response.isDone()) {
    // You can do some work that does not depend on the webservice
    System.out.println("isDone? " + response.isDone());
    Thread.sleep(1000);
    
}
CalculateResponse result = response.get();
System.out.println(result.getOut());

Client calling WS
Checking result
isDone? false
isDone? false
isDone? false
isDone? false
isDone? false
isDone? false
isDone? false
isDone? false
isDone? false
15

System.out.println("Client calling WS");
CalculatorSOAPProxy proxy = new CalculatorSOAPProxy();
proxy._getDescriptor().setEndpoint(
        "http://localhost:9089/WS2Server/Calculator");
CalcRequestType in = new CalcRequestType();
in.setOperand1(5);
in.setOperand2(10);
in.setOperation(OperationType.ADD);
/**
 * Using the callback model
 */

ResponseCallBackHandler handler = new ResponseCallBackHandler();
Future response = proxy.calculateAsync(in, handler);
while(!response.isDone()){
    System.out.println("isDone? "+response.isDone());
    Thread.sleep(1000);
}
System.out.println(handler.getCalResult());

import java.util.concurrent.ExecutionException;
import javax.xml.ws.AsyncHandler;
import javax.xml.ws.Response;
import com.server.calculator.CalculateResponse;

public class ResponseCallBackHandler implements
AsyncHandler {
 
 private String calResult;
 
 @Override
 public void handleResponse(Response res) {
  System.out.println("Handler invoked");
  try {
   CalculateResponse response = res.get();
   calResult = response.getOut();
   
  } catch (InterruptedException e) {
   e.printStackTrace();
  } catch (ExecutionException e) {
   e.printStackTrace();
  }
  
  
 }

 public void setCalResult(String calResult) {
  this.calResult = calResult;
 }

 public String getCalResult() {
  return calResult;
 }
}

Output logs:

Client calling WS
isDone? false
isDone? false
isDone? false
isDone? false
isDone? false
isDone? false
isDone? false
isDone? false
isDone? false
Handler invoked
15


public class TestServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;

 public TestServlet() {
  super();
 }

 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  System.out.println("Client calling WS");

  CalculatorSOAPProxy proxy = new CalculatorSOAPProxy();
  proxy._getDescriptor().setEndpoint(
    "http://localhost:9089/WS2Server/Calculator");
  BindingProvider bp = (BindingProvider) proxy._getDescriptor()
    .getProxy();
  bp.getResponseContext().put(
    "com.ibm.websphere.webservices.use.async.mep", Boolean.TRUE);

  CalcRequestType in = new CalcRequestType();
  in.setOperand1(5);
  in.setOperand2(10);
  in.setOperation(OperationType.ADD);
  ResponseCallBackHandler handler = new ResponseCallBackHandler();
  Future wsResponse = proxy.calculateAsync(in, handler);

  while (!wsResponse.isDone()) {
   System.out.println("isDone? " + wsResponse.isDone());
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  System.out.println(handler.getCalResult());
 }
}

[6/1/14 18:37:20:082 IST] 00000025 SystemOut O Client calling WS
[6/1/14 18:37:20:605 IST] 00000025 SystemOut O isDone? false
[6/1/14 18:37:20:644 IST] 00000070 WSChannelFram A CHFW0019I: The Transport Channel Service has started chain HttpOutboundChain:localhost:9089.
[6/1/14 18:37:20:729 IST] 00000024 SystemOut O 5
[6/1/14 18:37:20:730 IST] 00000024 SystemOut O 10
[6/1/14 18:37:20:730 IST] 00000024 SystemOut O ADD
[6/1/14 18:37:21:605 IST] 00000025 SystemOut O isDone? false
[6/1/14 18:37:22:605 IST] 00000025 SystemOut O isDone? false
[6/1/14 18:37:23:605 IST] 00000025 SystemOut O isDone? false
[6/1/14 18:37:24:605 IST] 00000025 SystemOut O isDone? false
[6/1/14 18:37:25:605 IST] 00000025 SystemOut O isDone? false
[6/1/14 18:37:26:605 IST] 00000025 SystemOut O isDone? false
[6/1/14 18:37:27:605 IST] 00000025 SystemOut O isDone? false
[6/1/14 18:37:28:605 IST] 00000025 SystemOut O isDone? false
[6/1/14 18:37:28:762 IST] 00000071 SystemOut O Handler invoked
[6/1/14 18:37:29:605 IST] 00000025 SystemOut O 15

3 comments:

  1. very nice and helpfull article.The translation service is really very helpful to me.
    access Mp3skull in UK

    ReplyDelete
  2. I read your article and get very important information in addition if you have any query you can click here. Holborn Assets

    ReplyDelete
  3. Nice post,it's very informative.i found the best information.I updated my knowledge with this blog.it can help me to crack GIS jobs in Hyderabad.

    ReplyDelete