Example An AsyncHandier Implementation
225 private static class RequestOrderCallbackHandler
226 implements AsyncHandler<RequestOrderResponse> {
228 private long startTime;
230 public void handleResponse (Response<RequestOrderResponse> response) {
232 long elapsed = (new Date()).getTime() - startTime;
233 Marshaller m;
235 JAXBContext jc = JAXBContext.newlnstance(RequestOrderResponse.class);
236 m = jc.createMarshaller();
237 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
238 System.out.println();
23 9 System.out.println("==============================================");
240 System.out.println("Asynchronous Proxy Test");
241 System.out.println("with Callback and Dynamic Service");
242 System.out.println("Elapsed waiting time for Web service response:");
243 System.out.println(elapsed + " milliseconds.");
244 System.out.println("==============================================");
245 System.out.println();
246 System.out.println();
247 System.out.println("Response Message =============================");
249 RequestOrderResponse orderResponse = response.get();
250 m.marshal(orderResponse, System.out);
252 } catch (ExecutionException e) {
253 Throwable t = e.getCause();
254 if ( t instanceof SOAPFaultException ) {
255 processSOAPFault((SOAPFaultException) t);
256 return;
258 e.printStackTrace();
260 e.printStackTrace();
265 public void setStartTime(long t) { startTime = t; }
book-code/chap06/asynchronous/modules/client/src/java/samples/Client.java
In addition, one other important function is performed by this handler—exception processing. As I illustrated in Section 6.1.4, it is very important to build fault handling into your Web services invocations. In this case, I show how to implement fault handling in a callback scenario, within an AsyncHandler<T> implementation. According to the JAX-WS specification, if a wsdl:operation asynchronous invocation fails, neither a SEI-specific exception nor a SOAPFaultException gets thrown directly; instead, it throws a java.util.concurrent.ExecutionException instance is returned from the Response.get() method. The cause of the ExecutionException, retrieved using the getCause() method, contains either the SEI-specific exception (e.g., inputFault from Example 6-4) or a protocol-specific exception such as SOAPFaultException.
In Example 6-24, I show the code to handle the SOAPFaultException case. In this case, I simply invoke the processSOAPFault method as in Example 6-8 to print out the contents of the SOAP fault message. You can run this example by following the same steps as shown in Example 6-21. The instructions there run both the polling and the callback invocations of the target Web service.
That concludes this discussion of client-side Web service invocation basics. I showed you how to use SEI proxies for Java style invocation, and how to use the Dispatch<T> interface for XML messaging. You saw both synchronous and asynchronous invocation styles. I also looked into some of the details of the JAX-WS WSDL to Java mapping and talked about the importance of fault processing when dealing with Web services invocation.
In the next section, I look at the topic of client-side handlers. Handlers enable you to do pre- and post-processing of the messages used to invoke Web services. Handlers can be used when doing SEI proxy style invocation or Dispatch<T> style XML messaging.
Post a comment