Home

 

Callback client

To implement an asynchronous invocation that uses the callback model, the client provides an AsynchHandler callback handler to accept and process the inbound response object. The client callback handler implements the javax.xml.ws.AsynchHandler interface, which contains the application code that is run when an asynchronous response is received from the server.

The AsynchHandler interface contains the handleResponse(Response) method that is called after the run time has received and processed the asynchronous response from the server. The response is delivered to the callback handler in the form of a javax.xml.ws.Response object. The response object returns the response content when the get method is called.

Additionally, if an error was received, then an exception is returned to the client during that call. The response method is then invoked according to the threading model used by the executor method, java.util.concurrent.Executor on the client's java.xml.ws.Service instance that was used to create the dynamic proxy or dispatch client instance. The executor is used to invoke any asynchronous callbacks registered by the application. Use the setExecutor and getExecutor methods to modify and retrieve the executor configured for the service.

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

Create the call back handler class RetrieveCustomerCallbackHandler in the itso.rad75.bank.test package and copy/paste the code from C:\7672code\webservice\thinclient (Example | 8-16).

Example 18-16 RetrieveCustomerCallbackHandler

package itso.rad75.bank.test;
import itso.rad75.bank.model.simple.RetrieveCustomerNameResponse;
import java.util.concurrent.ExecutionException;
import javax.xml.ws.AsyncHandler;
import javax.xml.ws.Response;
public class RetrieveCustomerCallbackHandler implements
								AsyncHandler<RetrieveCustomerNameResponse> {
	private String customerFullName;
	
	public void handleResponse(Response<RetrieveCustomerNameResponse> resp){
		try {
			RetrieveCustomerNameResponse rcnr = resp.get();
			customerFullName = rcnr.getCustomerFullName();
		} catch (ExecutionException e) {
			System.out.println(e.getCause());
		} catch (InterruptedException e) {
			System.out.println(e.getCause());
		}
	}
	public String getResponse() {
		return customerFullName;
	}
}

Create the call back client class BankCallbackClient in the itso.rad75.bank.test package and copy/paste the code from C:\7672code\webservice\thinclient (Example | 8-17).

Example 18-17 BankCallbackClient

package itso.rad75.bank.test;
import itso.rad75.bank.model.simple.BankPortProxy;
import java.util.concurrent.Future;
public class BankCallbackClient {
	
	public static void main(String[] args) throws Exception {
		BankPortProxy proxy = new BankPortProxy();
		// Set up the callback handler.
		RetrieveCustomerCallbackHandler callbackHandler = 
									new RetrieveCustomerCallbackHandler();
		// Make the Web service call.
		Future<?> response = proxy.retrieveCustomerNameAsync
										("111-11-1111", callbackHandler);
		System.out.println("Wait 5 seconds.");
		// Give the callback handler a chance to be called. 
		Thread.sleep(5000);
		System.out.println("Customer's full name is " 
									+ callbackHandler.getResponse() + ".");
		System.out.println("RetrieveCustomerName async end.");
	}
}

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

Wait 5 seconds.

Customer's full name is Mr. Henry Cui.

RetrieveCustomerName async end.

ibm.com/redbooks