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
public class AsyncBean {
private static final Logger logger = ...
public void ignoreResult(int a, int b) {
logger.info("it's asynchronous");
// here you can add an heavy payload!
}
public Future<Integer> longProcessing(int a, int b) {
return new AsyncResult<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...