Library Link To Toggle Frames Print Feedback

Implementing an Asynchronous Client with the Polling Approach

The polling approach is the more straightforward of the two approaches to developing an asynchronous application. The client invokes the asynchronous method called OperationNameAsync() and is returned a Response<T> object that it can poll for a response. What the client does while it is waiting for a response is up to the requirements of the application. There are two basic patterns for how to handle the polling:

Using the non-blocking pattern

Example 7.4, “Non-Blocking Polling Approach for an Asynchronous Operation Call” illustrates using non-blocking polling to make an asynchronous invocation on the greetMeSometime operation defined in Example 7.1, “WSDL Contract for Asynchronous Example”. The client invokes the asynchronous operation and periodically checks to see if the result has returned.

Example 7.4. Non-Blocking 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_world_async_soap_http.*;

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 {

    // set up the proxy for the client 

1    Response<GreetMeSometimeResponse> greetMeSomeTimeResp =
      port.greetMeSometimeAsync(System.getProperty("user.name"));

2      while (!greetMeSomeTimeResp.isDone()) {
      // client does some work
      }
3      GreetMeSometimeResponse reply = greetMeSomeTimeResp.get();
      // process the response 

      System.exit(0);
  }
}

The code in Example 7.4, “Non-Blocking Polling Approach for an Asynchronous Operation Call” does the following:

1

Invokes the greetMeSometimeAsync() on the proxy.

The method call returns the Response<GreetMeSometimeResponse> object to the client immediately. The Celtix Enterprise runtime handles the details of receiving the reply from the remote endpoint and populating the Response<GreetMeSometimeResponse> object.

[Note] Note

The runtime transmits the request to the remote endpoint's greetMeSometime() method and handles the details of the asynchronous nature of the call under the covers. The endpoint, and therefore the service implementation, never needs to worry about the details of how the client intends to wait for a response.

2

Checks to see if a response has arrived by checking the isDone() of the returned Response object.

If the response has not arrived, the client does some work before checking again.

3

If the response has arrived, the client retrieves it from the Response object using the get().

Using the blocking pattern

Using blocking polling to make asynchronous invocations on a remote operation follows the same steps as non-blocking polling. However, instead of using the Response object's isDone() to check if a response has been returned before calling the get() to retrieve the response, you immediately call the get(). The get() blocks until the response is available.

[Tip] Tip

You can also pass a timeout limit to the get() method.

Example 7.5, “Blocking Polling Approach for an Asynchronous Operation Call” shows a client that uses blocking polling.

Example 7.5. Blocking 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_world_async_soap_http.*;

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 {

    // set up the proxy for the client 

    Response<GreetMeSometimeResponse> greetMeSomeTimeResp =
      port.greetMeSometimeAsync(System.getProperty("user.name"));
    GreetMeSometimeResponse reply = greetMeSomeTimeResp.get();
      // process the response 
      System.exit(0);
  }
}