You can now specify a Session Bean (and/or a method) to be asynchronous. End-user perceived wait times can be significantly reduced with an asynchronous programming model that doesn’t force the complexity of AJAX, MoM or application-managed server threads.
With EJB 3.1 when you call the method from your client, the container will take the request and return immediately. Then it will dispatch the request to a bean instance within the pool of SLSBs.
The return type for async methods is either void or Future. The Future interface provides methods to check whether the operation has completed, gets the result – with options for a timeout and also cancel the operation. A concrete implementation of Future is AsyncResult which is available in the javax.ejb package.
Stateless EJBs may choose to return the AsyncResult object with any type returned. Transaction propagation is simple & limited - REQUIRED=REQUIRES_NEW attributes. If your client is already in a transaction, or your beans are in a transaction, the async method is called in a totally new transaction with no propagation (as in synchronous methods). This makes a lot of sense, because the entire purpose of asynchronous calls are to decouple a process orchestration to speed up things. However, it does complicate compensation and atomicity. Software architects need to trade-off between transaction demarcation and generally ACID properties across various calls. Any application exception that gets thrown by the bean for a void return type cannot be handled by the client. If your bean can encounter an exception and your client cares about handling it, then return a Future<V> form the bean. The client must handle the exception on a Future.get and getCause from ExecutionException.
An entire bean (itself or via it’s super class) can be marked as @Asynchronous. Sometimes it makes sense to expose the same business function as a synchronous and an asynchronous interface, using the @Asynchronous tag can provide the cohesion with a decoupled interaction model. This opens the door to multiple use-cases that need a simple way to conduct asynchronous calls without the complexity of MDBs or managing application owned threads. EJB 3.1 has built upon the POJO model for EJBs with an excellent way to realize asynchronous server-side models.