Java Reference
In-Depth Information
return -1;
}
private void cancelTimers() { [4]
for (Timer timer :
timerService.getTimers()) {
timer.cancel();
}
}
}
The first thing we should account for is the resource injection of the
Timer
object
[1]
that will be used in the
cancelTimers
method
[4]
to cancel all the scheduling
when the theatre is fully booked.
Next, pay attention to the
Schedule
annotation
[2]
we are using that will fire a non-
persistent timer each minute.
Note
Persistent timers
(the default option) are able to 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 nonpersistent timer
should be used, as in the preceding example.
When the 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 (obvi-
ously, we won't need to check the money for our automatic customer).