Sometimes it is useful to have some processing done asynchronously, that is, invoking a method call and returning control immediately to the client, without having the client wait for the method to finish.
In earlier versions of Java EE, the only way to invoke EJB methods asynchronously was to use message-driven beans (discussed in the next section). Although message-driven beans are fairly easy to write, they do require some configuration, such as setting up JMS message queues or topics, before they can be used.
EJB 3.1 introduced the @Asynchronous
annotation, which can be used to mark a method in a session bean as asynchronous. When an EJB client invokes an asynchronous method, control immediately goes back to the client, without waiting for the method to finish.
Asynchronous methods can only return void
or an implementation of the java.util.concurrent.Future
interface. The following example illustrates both scenarios:
package net.ensode.javaeebook.asynchronousmethods...