Java Reference
In-Depth Information
With future2 , we instead poll and block for five seconds only. For this second HTTP asyn-
chronous request, we let JAX-RS automatically unmarshal the HTTP response body into an
Order . java.util.concurrent.TimeoutException is thrown if the call takes longer than
five seconds. You can also invoke the nonblocking isDone() or isCancelled() methods on
Future to see if the request is finished or cancelled.
Exception handling
Exceptions that can be thrown by Future.get() methods are defined by that interface.
java.util.concurrent.TimeoutException occurs if we are calling Future.get() with a
timeout. InterruptedException happens if the calling thread has been interrupted.
java.util.concurrent.ExecutionException is a wrapper exception. Any exception
thrown by the JAX-RS runtime is caught and wrapped by this exception. Let's expand on the
future2 example to see how this works:
// Wait 5 seconds
try
try {
Order result2 = future2 . get ( 5 , TimeUnit . SECONDS );
} catch
catch ( TimeoutException timeout ) {
System . err . println ( "request timed out" );
} catch
catch ( InterruptedException ie ) {
System . err . println ( "Request was interrupted" );
} catch
catch ( ExecutionException ee ) {
Throwable cause = ee . getCause ();
iif ( cause instanceof
instanceof WebApplicationException ) {
( WebApplicationException ) wae = ( WebApplicationException ) cause ;
wae . close ();
} else
instanceof ResponseProcessingException ) {
ResponseProcessingException rpe = ( ResponseProcessingException ) cause ;
rpe . close ();
} else
else iif ( cause instanceof
instanceof ProcessingException ) {
// handle processing exception
} else
else iif ( cause instanceof
else {
// unknown
}
}
You can obtain any exception thrown by the JAX-RS runtime when an asynchronous request
is executed by calling the ExecutionException.getCause() method. The possible
wrapped JAX-RS exceptions are the same as the synchronous ones discussed in Chapter 8 .
In the example, the call to future2.get() unmarshalls the response to an Order object. If
the response is something other than 200, “OK,” then the JAX-RS runtime throws one of the
Search WWH ::




Custom Search