Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
titlePolling 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_world_async_soap_http.GreeterAsync;
import org.apache.hello_world_async_soap_http.SOAPService;
import org.apche.hello_world_async_soap_http.types.GreetMeSometimeResponse;

public final class Client {
  private static final QName SERVICE_NAME
    = new QName("http://objectweb.org/hello_world_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:

    Code Block
    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:

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

Implementing an asynchronous client with the callback approach

...