An alternative approach to making an asynchronous operation invocation is to implement a callback class. You then call the asynchronous remote method that takes the callback object as a parameter. The runtime returns the response to the callback object.
To implement an application that uses callbacks, do the following:
Create a callback class that implements the AsyncHandler
interface.
![]() | Note |
---|---|
Your callback object can perform any amount of response processing required by your application. |
Make remote invocations using the
that takes the callback object as a parameter and returns a operationName
Async()Future<?>
object.
If your client requires access to the response data, you can poll the returned
Future<?>
object's isDone()
method to see if the remote
endpoint has sent the response.
![]() | Tip |
---|---|
If the callback object does all of the response processing, it is not necessary to check if the response has arrived. |
The callback class must implement the javax.xml.ws.AsyncHandler
interface. The
interface defines a single method:
void handleResponse(Response<T> res);
The FUSE Services Framework runtime calls the handleResponse()
method to notify the client that the
response has arrived. Example 18.7 shows an outline of the
AsyncHandler
interface that you must implement.
Example 18.7. The javax.xml.ws.AsyncHandler
Interface
public interface javax.xml.ws.AsyncHandler { void handleResponse(Response<T> res) }
Example 18.8 shows a callback class for the greetMeSometime operation defined in Example 18.1.
Example 18.8. Callback Implementation Class
package demo.hw.client; import javax.xml.ws.AsyncHandler; import javax.xml.ws.Response; import org.apache.hello_world_async_soap_http.types.*; public class GreeterAsyncHandler implements AsyncHandler<GreetMeSometimeResponse> {private GreetMeSometimeResponse reply;
public void handleResponse(Response<GreetMeSometimeResponse> response) { try { reply = response.get(); } catch (Exception ex) { ex.printStackTrace(); } }
public String getResponse() { return reply.getResponseType(); } }
The callback implementation shown in Example 18.8 does the following:
Defines a member variable, | |
Implements This implementation simply extracts the response and assigns it to the member variable | |
Implements an added method called This method is a convenience method that extracts the data from |
Example 18.9 illustrates a client that uses the callback approach to make an asynchronous call to the GreetMeSometime operation defined in Example 18.1.
Example 18.9. Callback 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 { ... public static void main(String args[]) throws Exception { ... // Callback approachGreeterAsyncHandler callback = new GreeterAsyncHandler();
Future<?> response = port.greetMeSometimeAsync(System.getProperty("user.name"), callback);
while (!response.isDone()) { // Do some work }
resp = callback.getResponse(); ... System.exit(0); } }
The code in Example 18.9 does the following:
Instantiates a callback object. | ||||
Invokes the The method call returns the
| ||||
Uses the returned | ||||
Invokes the callback object's |