Adding asynchronous methods to our EJBs
Before the EJB 3.1 specification, the only way to provide asynchronous capabilities to en-
terprise applications was using message-driven bean recipes. This remains substantially a
best practice, and we are going to discuss this in depth in Chapter 6 , Developing Applica-
tions with JBoss JMS Provider ; however, in some cases, it might be desirable (and easier)
to use these asynchronous features from a component that follows the classical request-
You can make the EJB's method asynchronous by simply tagging it with the @Asyn-
chronous annotation. Each time this method is invoked, it will immediately return, re-
gardless of how long the method actually takes to complete.
This can be used in one of two ways:
• The first technique is a fire-and-forget manner, where the request is made up of the
EJB and the client is not concerned about the success or failure of the request.
• The second modus operandi invokes the method but does not wait for the method
to be completed. The method returns a Future object. This object is used later to
determine the result of the request.
Using fire-and-forget asynchronous calls
If you don't care about the async result, you can just have your async method return void.
For this purpose, we will add a new method named bookSeatAsync to
TheatreBooker and simply tag it as @Asynchronous . This is shown in the following
public void bookSeatAsync(int seatId) throws
As you can see, this method does not return anything; it just executes our synchronous
bookSeet method. We will need to use some other instruments to check whether the
transaction was completed successfully. For example, we can check from the theatre list
whether the seat has been booked successfully.
Search WWH ::