Making asynchronous call to REST service

In the Using javax.xml.ws.Dispatch for REST Client entry i talked about how you can use the javax.xml.ws.Dispatch class for making call to the REST service. As you can see it makes calling the REST service difficult and it might not make sense to use this approach for calling service in most cases but it might be good idea to think about this approach when you want to make asynchronous calls, i wanted to try this approach so i made following changes to the DispatcherTest1.java

package com.webspherenotes.jaxrs;

import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.http.HTTPBinding;

public class DispatcherTest1 {

  public static void main(String[] args) {
    try {
      QName serviceName = new QName("http://www.webspherenotes.com/rest", "svc");
      QName portName = new QName("http://www.webspherenotes.com/rest", "port");
      Service svc = Service.create(serviceName);
      svc.addPort(portName, HTTPBinding.HTTP_BINDING, 
   "http://localhost:9080/MyWebService/rest/contact/1");
      Dispatch dis =
        svc.createDispatch(portName, Source.class, Service.Mode.PAYLOAD);
      Map requestContext = dis.getRequestContext();
      requestContext.put(MessageContext.HTTP_REQUEST_METHOD, "GET");
      
      Future f= dis.invokeAsync(null, new DemoAyncHandler());
      System.out.println("After making async call to request");
      f.get();
    
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ExecutionException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }

}
We have to change only couple of lines here, first change the Dispatcher method that your calling from invoke to invokeAsync. The invokeAsync method returns object of java.util.concurrent.Future, calls its get method so that the current thread does not end before it gets calls response callback. Then create DemoAsyncHandler class that gets called when the response for REST service is ready.


package com.test;

import java.util.concurrent.ExecutionException;

import javax.xml.transform.Source;
import javax.xml.ws.AsyncHandler;
import javax.xml.ws.Response;

public class DemoAyncHandler implements AsyncHandler{

  @Override
  public void handleResponse(Response response) {
    try {
      System.out.println("Inside DemoAsyncHandler.handleResponse "
   + response);
      Source source = response.get();
      XMLHelper.prettyPrintXML(source, System.out);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ExecutionException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

}
Only thing that i am doing in the DemoAsyncHandler is to print the response on console, this is how the output looks like

1 comment:

scoot said...


THANK YOU FOR THE INFORMATION
PLEASE VISIT US
Web Design Company in Bangalore