Implementing an asynchronous client with the callback approach - 8.0

Talend ESB Service Developer Guide

Version
8.0
Language
English
Product
Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
Module
Talend ESB
Talend Runtime
Content
Design and Development
Installation and Upgrade
Last publication date
2023-11-06

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 .