Java Reference
In-Depth Information
session beans is a pretty heavyweight solution that for simple tasks is overkill and adds un-
necessary complexity.
The title asynchronous session beans is somewhat of a misnomer. The beans aren't asyn-
chronous in themselves; rather, their methods are asynchronous. With the @Asynchron-
ous annotation, a method or an entire class can be marked as being asynchronous. When
an asynchronous method or method on an asynchronous class is invoked, the container
spawns a separate thread for execution.
3.5.1. Basics of asynchronous invocation
An asynchronous method is just like any other business method on a bean class—it can
return a value, throw exceptions, and accept parameters. The return type for an asynchron-
ous method is either void or java.util.concurrent.Future<V> . This enables
two usage patterns: fire and forget (using void ) or fire and check back later for an answer
( Future<V> ). Only methods with a declared return type of Future<V> can declare an
exception. If an exception is thrown by the asynchronous method, the exception will be
caught and rethrown when get() is called on the Future object. The original exception
will be wrapped in an ExecutionException . Runtime exceptions thrown by an asyn-
chronous method with no return type will be lost and the original caller will not be notified.
One important caveat of asynchronous method invocation is that transactions aren't
propagated to the asynchronous method—a new transaction will be started for the asyn-
chronous method. But unlike transactions, the security principle will be propagated. We'll
cover transactions and security in great detail in chapter 6 .
When an asynchronous method is invoked, the call returns to the client before the method
is invoked on the bean. If no value is returned from the method (the return type is void) ,
the operation is a fire-and-forget task from the perspective of the client. If you might need
the result of asynchronous processing or might have to potentially cancel the operation,
a Future object should be returned by the business method. In both circumstances, the
execution proceeds on a separate thread. This is incredibly powerful; the next section will
explain the ground rules for when and where you should add asynchronous processing to
an application.
Search WWH ::




Custom Search