Java Reference
In-Depth Information
Adding asynchronous methods to our EJBs
Before the EJB 3.1 specification, the only way to provide asynchronous capabilities
to enterprise applications was using message-driven bean recipes. This remains
substantially a best practice, and we are going to discuss this in depth in Chapter 7 ,
Developing Applications 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-reply pattern.
You can make an EJB's method asynchronous by simply tagging it with the @Asyn-
chronous annotation. Each time this method is invoked, it will immediately return,
regardless of how long the method actually takes.
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 complete. 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
TheatreBookerBean and simply tag it as @Asynchronous .
@Asynchronous
public void bookSeatAsync(int seatId) {
Seat seat =
theatreBox.getSeatList().get(seatId);
if (seat.isBooked()) {
throw new SeatBookedException("Seat Already
booked!"); }
Search WWH ::




Custom Search