3-26 Programming Advanced Features of JAX-WS Web Services for Oracle WebLogic Server
■
Use the McFeature methods defined in Table 3–13
. For example: ...
McFeature mcFeature = new McFeature; mcFeature.setUseMCWithSyncInvoketrue;
MyService port = service.getMyServicePortmcFeature; ...
You can set the maximum amount of time a synchronous method will block and wait for a response using the weblogic.wsee.jaxws.JAXWSProperties.REQUEST_
TIMEOUT property. This property default to 0 indicating no timeout. For more information about setting message properties, see
Section 3.5.3, Propagating User-defined Request Context to the Response
.
3.7 Using the JAX-WS Reference Implementation
The JAX-WS Reference Implementation RI supports the following programming models:
■
Asynchronous client polling through use of the java.util.concurrent.Future interface.
■
Asynchronous callback handlers on a per request basis. The calling client specifies the callback handler at invocation time. When the response is available, the
callback handler is invoked to process the response.
Unlike with asynchronous client transport feature, the JAX-WS RI provides very limited support for WS-Addressing, including:
■
Manual support for adding client-side outbound WS-Addressing headers.
■
Manual support for publishing the client-side endpoint to receive responses.
■
No support for detecting incorrect client-side programming model resulting in synchronous call hanging, for example.
■
No support for surviving a client-side or service-side restart. The following example shows a simple client file, AsyncClient, that has a single
method, AddNumbersTestDrive, that asynchronously invokes the AddNumbersAsync method of the AddNumbersService service. The Java code in
bold
is described following the code sample. package examples.webservices.async.client;
import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit;
import javax.xml.ws.BindingProvider;
Table 3–13 Methods for Configuring Synchronous Method Support
Method Description
boolean isUseMCWithSyncInvoke Returns a boolean value indicating whether
synchronous method support is enabled. void setUseMCWithSyncInvokeboolean
useMCWithSyncInvoke Sets the synchronous method support flag.
Valid values are true and false. This flag defaults to false.
Invoking Web Services Asynchronously 3-27
import java.util.concurrent.Future; import javax.xml.ws.AsyncHandler;
import javax.xml.ws.Response; public class AsyncClient {
private AddNumbersPortType port = null; protected void setUp throws Exception {
AddNumbersService service = new AddNumbersService; port = service.getAddNumbersPort;
String serverURI = System.getPropertywls-server; BindingProvider port.getRequestContext.put
BindingProvider.ENDPOINT_ADDRESS_PROPERTY, http: + serverURI + JAXWS_ASYNCAddNumbersService;
}
Asynchronous callback handler class AddNumbersCallbackHandler implements AsyncHandlerAddNumbersResponse
{ private AddNumbersResponse output;
public void handleResponseResponseAddNumbersResponse response { try {
output = response.get; } catch ExecutionException e {
e.printStackTrace; } catch InterruptedException e {
e.printStackTrace; }
} AddNumbersResponse getResponse {
return output; }
}
public void AddNumbersTestDrive throws Exception { int number1 = 10;
int number2 = 20; Asynchronous Callback method
AddNumbersCallbackHandler callbackHandler = new AddNumbersCallbackHandler;
Future? resp = port.addNumbersAsyncnumber1, number2, callbackHandler;
For the purposes of a test, block until the async call completes resp.get5L, TimeUnit.MINUTES;
int result = callbackHandler.getResponse.getReturn;
Polling method ResponseAddNumbersResponse addNumbersResp =
port.AddNumbersAsyncnumber1, number2; while addNumbersResp.isDone {
Thread.sleep100; }
AddNumbersResponse reply = addNumbersResp.get; System.out.printlnServer responded through polling with: +
reply.getResponseType; }
}
3-28 Programming Advanced Features of JAX-WS Web Services for Oracle WebLogic Server
The example demonstrates the steps to implement both the asynchronous polling and asynchronous callback handler programming models.
To implement an asynchronous callback handler:
1.
Create an asynchronous handler that implements the javax.xml.ws.AsyncHandlerT interface see
http:download.oracle.comjavaee5apijavaxxmlwsAsyncHan dler.html
. The asynchronous handler defines one method, handleResponse, that enables clients to receive callback notifications at the completion of service
endpoint operations that are invoked asynchronously. The type should be set to AddNumberResponse.
class AddNumbersCallbackHandler implements AsyncHandlerAddNumbersResponse { private AddNumbersResponse output;
public void handleResponseResponseAddNumbersResponse response { try {
output = response.get; } catch ExecutionException e {
e.printStackTrace; } catch InterruptedException e {
e.printStackTrace;
} }
AddNumbersResponse getResponse { return output;
} }
2.
Instantiate the asynchronous callback handler. AddNumbersCallbackHandler callbackHandler =
new AddNumbersCallbackHandler;
3.
Instantiate the AddNumbersService Web service and call the asynchronous version of the Web service method, addNumbersAsync, passing a handle to the
asynchronous callback handler.
AddNumbersService service = new AddNumbersService; port = service.getAddNumbersPort;
...
Future? resp = port.addNumbersAsyncnumber1, number2, callbackHandler;
java.util.concurrent.Future see http:download.oracle.comjavase1.5.0docsapijavautilco
ncurrentFuture.html represents the result of an asynchronous computation
and provides methods for checking the status of the asynchronous task, getting the result, or canceling the task execution.
4.
Get the result of the asynchronous computation. In this example, a timeout value is specified to wait for the computation to complete.
resp.get5L, TimeUnit.MINUTES;
5.
Use the callback handler to access the response message.
Invoking Web Services Asynchronously 3-29
int result = callbackHandler.getResponse.getReturn; To implement an asynchronous polling mechanism:
1.
Instantiate the AddNumbersService Web service and call the asynchronous version of the Web service method, addNumbersAsync.
ResponseAddNumbersResponse addNumbersResp = port.AddNumbersAsyncnumber1, number2;
2.
Sleep until a message is received. while addNumbersResp.isDone {
Thread.sleep100;
3.
Poll for a response. AddNumbersResponse reply = addNumbersResp.get;
3.8 Propagating Request Context to the Response