Skip to main content

Implementing an asynchronous client with the polling approach

The next example illustrates the polling 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 javax.xml.ws.Response<T> object, where T is the type of the operation's response message. The Response<T> object can be polled at a later stage to check whether the operation's response message has arrived.

Polling 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://objectweb.org/hello_async_soap_http", 
      "SOAPService");

   private Client() {}

   public static void main(String args[]) throws Exception {
      ...
      // Polling approach:
      Response<GreetMeSometimeResponse> greetMeSomeTimeResp =
         port.greetMeSometimeAsync(System.getProperty("user.name"));
      while (!greetMeSomeTimeResp.isDone()) {
         Thread.sleep(100);
      }
      GreetMeSometimeResponse reply = greetMeSomeTimeResp.get();
      ...
      System.exit(0);
   }
}

The greetMeSometimeAsync() method invokes the greetMeSometimes operation, transmitting the input parameters to the remote service and returning a reference to a javax.xml.ws.Response<GreetMeSometimeResponse> object. The Response class is defined by extending the standard java.util.concurrency.Future<T> interface, which is specifically designed for polling the outcome of work performed by a concurrent thread. There are essentially two basic approaches to polling using the Response object:

  • Non-blocking polling - before attempting to get the result, check whether the response has arrived by calling the non-blocking Response<T>.isDone() method. For example:

    Response<GreetMeSometimeResponse> greetMeSomeTimeResp = ...;
    
    if (greetMeSomeTimeResp.isDone()) {
       GreetMeSometimeResponse reply = greetMeSomeTimeResp.get();
    }
  • Blocking polling - call Response<T>.get() right away and block until the response arrives (optionally specifying a timeout). For example, to poll for a response, with a 60 second timeout:

    Response<GreetMeSometimeResponse> greetMeSomeTimeResp = ...;
    
    GreetMeSometimeResponse reply = greetMeSomeTimeResp.get(60L,
       java.util.concurrent.TimeUnit.SECONDS);

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 – let us know how we can improve!