Skip to main content

Implementing an asynchronous client with the callback approach

An alternative approach to making an asynchronous operation invocation is to implement a callback class, by deriving from the javax.xml.ws.AsyncHandler interface. This callback class must implement a handleResponse() method, which is called by the CXF runtime to notify the client that the response has arrived. The below example shows an outline of the AsyncHandler interface that you need to implement.

The javax.xml.ws.AsyncHandler Interface

package javax.xml.ws;

public interface AsyncHandler<T> {
   void handleResponse(Response<T> res);
}

In this example, a callback class, TestAsyncHandler , is defined as shown in the example below.

The TestAsyncHandler Callback Class

package demo.hw.client;

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

import org.apache.hello_async_soap.types.GreetMeSometimeResponse;

public class TestAsyncHandler implements 
   AsyncHandler<GreetMeSometimeResponse> {
   private GreetMeSometimeResponse reply;

   public void handleResponse(Response<GreetMeSometimeResponse> 
      response) {
      try {
         reply = response.get();
      } catch (Exception ex) {
      ex.printStackTrace();
   }
}

   public String getResponse() {
      return reply.getResponseType();
   }
}

The implementation of handleResponse() shown in Example11 simply gets the response data and stores it in a member variable, reply . The extra getResponse() method is just a convenience method that extracts the sole output parameter (that is, responseType ) from the response.

Example12 illustrates the callback approach to making an asynchronous operation call. Using this approach, the client invokes the operation by calling the special Java method, _OperationName_Async() , that returns a java.util.concurrency.Future<?> object and takes an extra parameter of AsyncHandler<T> .

Callback Approach for an Asynchronous Operation Call

package demo.hw.client;

import java.io.File;
import java.util.concurrent.Future;

import javax.xml.namespace.QName;
import javax.xml.ws.Response;

import org.apache.hello_async_soap_http.GreeterAsync;
import org.apache.hello_async_soap_http.SOAPService;
import org.apache.hello_async_soap_http.types.GreetMeSometimeResponse;

public final class Client {
   private static final QName SERVICE_NAME
      = new QName("http://apache.org/hello_world_async_soap_http", 
      "SOAPService");

   private Client() {}

   public static void main(String args[]) throws Exception {
       ...
       // Callback approach
       TestAsyncHandler testAsyncHandler = new TestAsyncHandler();
       System.out.println(
          "Invoking greetMeSometimeAsync using callback object...");
       Future<?> response = port.greetMeSometimeAsync(
          System.getProperty("user.name"), testAsyncHandler);
       while (!response.isDone()) {
         Thread.sleep(100);
       }
       resp = testAsyncHandler.getResponse();
       ...
       System.exit(0);
   }
}

The Future<?> object returned by greetMeSometimeAsync() can be used only to test whether or not a response has arrived yet - for example, by calling response.isDone() . The value of the response is only made available to the callback object, testAsyncHandler .

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!