Invoking Web Services Asynchronously 3-15
The following example configures the asynchronous response endpoint address and enables use of asynchronous client transport for synchronous operations:
String responseAddress = http:localhost:7001myReliableServicemyReliableResponseEndpoint; boolean useAsyncWithSyncInvoke = true;
AsyncClientTransportFeature asyncFeature = new AsyncClientTransportFeatureresponseAddress, useAsyncWithSyncInvoke;
BackendService port = _service.getBackendServicePortasyncFeature;
3.5.2 Developing the Asynchronous Handler Interface
As described in Section 3.4, Building the Client Artifacts for Asynchronous Web
Service Invocation , the asynchronous handler interface,
weblogic.jws.jaxws.client.async.AsyncClientHandlerFeature, sets a single asynchronous handler instance on the port rather than on a per-request basis.
For example, when you build the client classes using clientgen, as described in Section 3.4, Building the Client Artifacts for Asynchronous Web Service Invocation
, the asynchronous handler interface is generated, as shown below.
Example 3–1 Example of the Asynchronous Handler Interface
import javax.xml.ws.Response; This class was generated by the JAX-WS RI.
Oracle JAX-WS 2.1.5 Generated source version: 2.1
public interface BackendServiceAsyncHandler { param response
public void onDoSomethingResponseResponseDoSomethingResponse response; }
The asynchronous handler interface is generated as part of the same package as the port interface and represents the methods required to accept responses for any
operation defined on the service. You can import and implement this interface in your client code to provide a way to receive and process asynchronous responses in a
strongly-typed manner.
To set a single asynchronous handler instance on the port, pass an instance of the weblogic.jws.jaxws.client.async.AsyncClientHandlerFeature as a
Note: If you set a single asynchronous handler instance on the port,
as described in this section, and subsequently attempt to configure a per-request asynchronous handler, as described in
Section 3.7, Using the JAX-WS Reference Implementation
, then a runtime exception is returned.
3-16 Programming Advanced Features of JAX-WS Web Services for Oracle WebLogic Server
parameter when creating the Web service proxy or dispatch. You specify the name of the asynchronous handler that will be invoked when a response message is received.
The following example shows how to develop an asynchronous handler interface. The example demonstrates how to initialize the AsyncClientHandlerFeature to
connect the asynchronous handler implementation to the port used to make invocations on the backend service. This example is excerpted from
Example 2–2, Asynchronous Web Service Client Best Practices Example
.
Example 3–2 Example of Developing the Asynchronous Handler Interface
import weblogic.jws.jaxws.client.async.AsyncClientHandlerFeature; ...
BackendServiceAsyncHandler handler = new BackendServiceAsyncHandler { public void onDoSomethingResponseResponseDoSomethingResponse res {
... Handle Response ... try {
DoSomethingResponse response = res.get; _lastResponse = response.getReturn;
System.out.printlnGot async response: + _lastResponse; } catch Exception e {
_lastResponse = e.toString; e.printStackTrace;
} }
}; AsyncClientHandlerFeature handlerFeature = new AsyncClientHandlerFeaturehandler;
features.addhandlerFeature; _features = features.toArraynew WebServiceFeature[features.size];
BackendService anotherPort = _service.getBackendServicePort_features; ...
Make the invocation. Our asynchronous handler implementation set into the AsyncClientHandlerFeature above receives the response.
String request = Dance and sing; System.out.printlnInvoking DoSomething asynchronously with request: + request;
anotherPort.doSomethingAsyncrequest;
3.5.3 Propagating User-defined Request Context to the Response