Java Reference
In-Depth Information
Calling Asynchronous Methods from Enterprise Bean Clients
Session bean clients call asynchronous methods just like non-asynchronous business
methods. If the asynchronous method returns a result, the client receives a
Future<V>
instance as soon as the method is invoked. This instance can be used to retrieve the final
result, cancel the invocation, check whether the invocation has completed, check wheth-
er any exceptions were thrown during processing, and check whether the invocation was
cancelled.
Retrieving the Final Result from an Asynchronous Method Invocation
The client may retrieve the result using one of the
Future<V>.get
methods. If pro-
cessing hasn't been completed by the session bean handling the invocation, calling one of
the
get
methods will result in the client halting execution until the invocation completes.
Use the
Future<V>.isDone
method to determine whether processing has completed
before calling one of the
get
methods.
The
get()
method returns the result as the type specified in the type value of the
Future<V>
instance. For example, calling
Future<String>.get()
will return
a
String
object. If the method invocation was cancelled, calls to
get()
result in
a
java.util.concurrent.CancellationException
being thrown. If the in-
vocation resulted in an exception during processing by the session bean, calls to
get()
result in a
java.util.concurrent.ExecutionException
being thrown. The
cause of the
ExecutionException
may be retrieved by calling the
ExecutionEx-
ception.getCause
method.
The
get(long timeout, java.util.concurrent.TimeUnit unit)
meth-
od is similar to the
get()
method, but allows the client to set a timeout value. If the
timeout value is exceeded, a
java.util.concurrent.TimeoutException
is
thrown. See the Javadoc for the
TimeUnit
class for the available units of time to specify
the timeout value.
Cancelling an Asynchronous Method Invocation
Call the
cancel(boolean mayInterruptIfRunning)
method on the
Fu-
ture<V>
instance to attempt to cancel the method invocation. The
cancel
method re-
turns
true
if the cancellation was successful, and
false
if the method invocation can-
not be cancelled.
When the invocation cannot be cancelled, the
mayInterruptIfRunning
parameter
is used to alert the session bean instance on which the method invocation is running that
the client attempted to cancel the invocation. If
mayInterruptIfRunning
is set to