Java Reference
In-Depth Information
@Asynchronous
public Future<String> processPayment(Order order) throws PaymentEx-
ception {
...
}
This method will attempt to process the payment of an order, and return the status as a
String
. Even if the payment processor takes a long time, the client can continue work-
ing, and display the result when the processing finally completes.
The
javax.ejb.AsyncResult<V>
class is a concrete implementation of the
Fu-
ture<V>
interface provided as a helper class for returning asynchronous results.
Asyn-
cResult
has a constructor with the result as a parameter, making it easy to create
Fu-
ture<V>
implementations. For example, the
processPayment
method would use
AsyncResult
to return the status as a
String
:
@Asynchronous
public Future<String> processPayment(Order order) throws PaymentEx-
ception {
...
String status = ...;
return new AsyncResult<String>(status);
}
The result is returned to the enterprise bean container, not directly to the client, and
the enterprise bean container makes the result available to the client. The session bean
can check whether the client requested that the invocation be cancelled by calling the
javax.ejb.SessionContext.wasCancelled
method. For example:
@Asynchronous
public Future<String> processPayment(Order order) throws PaymentEx-
ception {
...
if (SessionContext.wasCancelled()) {
// clean up
} else {
// process the payment
}
...
}