The asynchronous annotation
The simplest thing to make an EJB asynchronous is through the @Asynchronous
annotation. If a client calls an EJB marked with this annotation, it must not wait for the result because all the operations of the EJB will work in asynchronous mode. Here's a sample of an asynchronous singleton EJB:
@Singleton @Asynchronous publicclass AsyncBean { privatestaticfinal Logger logger = ... publicvoid ignoreResult(inta, intb) { logger.info("it's asynchronous"); // here you can add an heavy payload! } public Future<Integer> longProcessing(inta, intb) { returnnewAsyncResult<Integer>(a * b); } }
In this example, we have two methods--one void and one returning a result. The ignoreResult
method will be executed in a few milliseconds despite a large amount of data being loaded. All the loading will be executed in an asynchronous mode.
The longProcessing
method returns a javax.ejb.AsyncResult
class. This class is a simple implementation of typed Future
class that...