Home

 

Polling client

Using the polling model, a client can issue a request and receive a response object that can subsequently be polled to determine if the server has responded. When the server responds, the actual response can then be retrieved. The response object returns the response content when the get method is called. The client receives an object of type javax.xml.ws.Response from the invokeAsync method. That Response object is used to monitor the status of the request to the server, determine when the operation has completed, and to retrieve the response results.

To create an asynchronous Web service client using the polling model, do the following steps:

In the Java EE perspective, Services view, expand JAX-WS, then right-click RAD75WebServiceWeb: {http://.../}BankService and select Generate Æ Client.

Keep the slider at Deploy client level. Click the hyperlink Client project:, and in the Specify Client Project Settings dialog, select RAD75WebServiceThinClient, and click OK. Click Next.

Select Enable asynchronous invocation for generated client (Figure | 8-29) and click Finish.

Figure 18-29 Enable asynchronous invocation for generated client

After the code generation, open BankPortProxy.java (Example | 8-14).
For each method in the Web service, two additional methods are created. These are polling and callback methods, which allow the client to function asynchronously. The retrieveCustomerNameAsync method that returns a Response is used for polling, while the method that returns a Future is used for callback.

Example 18-14 BankPortProxy asynchronous methods

public Response<RetrieveCustomerNameResponse>
						retrieveCustomerNameAsync(String ssn) {
    return _getDescriptor().getProxy().retrieveCustomerNameAsync(ssn);
}
public Future<?> retrieveCustomerNameAsync(String ssn,
					AsyncHandler<RetrieveCustomerNameResponse> asyncHandler) {
    return _getDescriptor().getProxy().retrieveCustomerNameAsync
																(ssn,asyncHandler);
}

Create a new class called BankPollingClient in the itso.rad75.bank.test package and copy/paste the code from C:\7672code\webservice\thinclient (Example | 8-15).

Example 18-15 BankPollingClient

package itso.rad75.bank.test;
import itso.rad75.bank.model.simple.BankPortProxy;
import itso.rad75.bank.model.simple.RetrieveCustomerNameResponse;
import java.util.concurrent.ExecutionException;
import javax.xml.ws.Response;
public class BankPollingClient {
	public static void main(String[] args) {
		try {
			BankPortProxy proxy = new BankPortProxy();
			Response<RetrieveCustomerNameResponse> resp =
							proxy.retrieveCustomerNameAsync("111-11-1111");
			// Poll for the response.
			while (!resp.isDone()) {
				// You can do some work that does not depend on the customer
					name being available
				// For this example, we just check if the result is available
					every 0.2 seconds.
				System.out.println
						("retrieveCustomerName async still not complete.");
				Thread.sleep(200);
			}
			RetrieveCustomerNameResponse rcnr = resp.get();
			System.out.println
						("retrieveCustomerName async invocation complete.");
			System.out.println("Customer's name is " +
									rcnr.getCustomerFullName());
		} catch (InterruptedException e) {
			System.out.println(e.getCause());
		} catch (ExecutionException e) {
			System.out.println(e.getCause());
		}
	}
}

Right-click BankPollingClient.java and select Run As Æ Java Application. The output is written to the console:

retrieveCustomerName async still not complete.

retrieveCustomerName async still not complete.

retrieveCustomerName async invocation complete.

Customer's name is Mr. Henry Cui

ibm.com/redbooks