Java Reference
In-Depth Information
}
}
The first thing we should account for is the resource injection of the
Timer
object
[1]
,
which will be used in the
cancelTimers
method
[4]
to cancel all the scheduling
when the theatre is fully booked. Please note that the
timerService.getTimers()
method retrieves all active timers associated only with the current bean. In order to get all
timers from your application module, you have to use the
timerSer-
vice.getAllTimers()
method, which was added recently in EJB 3.2.
Next, pay attention to the
Schedule
annotation
[2]
we are using, which will fire a non-
persistent timer each minute.
Note
Persistent timers (the default option) can survive application and server crashes. When the
system recovers, any persistent timers will be recreated and missed callback events will be
executed.
When a replay of missed timer events is not desired, a non-persistent timer should be
used, as shown in the preceding example.
When an action is fired, the
automaticCustomer
method starts scanning the theatre
seats for an available seat. (Nothing too complex;
findSeat
starts looking from the first
available seat.)
Finally, if there are seats still available, the
buyTicket
method
[3]
of the
TheatreBox
singleton will be used to short circuit the purchase of the seat (obviously,
we won't need to check the money for our automatic customer).