Java Reference
In-Depth Information
Chapter 13. Using Asynchronous Method Invocation in
Session Beans
This chapter discusses how to implement asynchronous business methods in session beans
and call them from enterprise bean clients.
The following topics are addressed here:
• “ Asynchronous Method Invocation on page 241
• “ The async Example Application on page 244
Asynchronous Method Invocation
Session beans can implement asynchronous methods , business methods where control is
returned to the client by the enterprise bean container before the method is invoked on the
session bean instance. Clients may then use the Java SE concurrency API to retrieve the
result, cancel the invocation, and check for exceptions. Asynchronous methods are typic-
ally used for long-running operations, for processor-intensive tasks, for background tasks,
to increase application throughput, or to improve application response time if the method
invocation result isn't required immediately.
When a session bean client invokes a typical non-asynchronous business method, control
is not returned to the client until the method has completed. Clients calling asynchronous
methods, however, immediately have control returned to them by the enterprise bean
container. This allows the client to perform other tasks while the method invocation
completes. If the method returns a result, the result is an implementation of the
java.util.concurrent.Future<V> interface, where “V” is the result value type.
The Future<V> interface defines methods the client may use to check whether the com-
putation is completed, wait for the invocation to complete, retrieve the final result, and can-
cel the invocation.
Creating an Asynchronous Business Method
Annotate a business method with javax.ejb.Asynchronous to mark that method as
an asynchronous method, or apply @Asynchronous at the class level to mark all the
business methods of the session bean as asynchronous methods. Session bean methods that
expose web services can't be asynchronous.
Asynchronous methods must return either void or an implementation of the Future<V>
interface. Asynchronous methods that return void can't declare application exceptions,
but if they return Future<V> , they may declare application exceptions. For example:
Search WWH ::




Custom Search