Java Reference
In-Depth Information
TimerConfig());
}
Next, we will create a callback method named
timeout
and use the
@Timeout
annota-
tion on top of the method. In the
timeout
method, we could, for example, reinitialize
our singleton by invoking the
setupTheatre
method. Nothing fancy; however, this
should give you an idea of how to get working with a single action timer. Refer to the fol-
lowing code for more information:
@Timeout
public void timeout(Timer timer){
logger.info("Re-building Theatre Map.");
setupTheatre();
}
Scheduling timer events
If you want to schedule timed notifications at fixed intervals, the simplest way is to use
the
@Schedule
annotation. The
@Schedule
annotation takes a series of comma-de-
limited settings to express a time period or set of time periods, much as the Unix
cron
utility does. Each setting corresponds to a unit of time such as an hour or minute. A
simple repeating event occurring every minute can be expressed using the
@Schedule
annotation as follows:
@Schedule(second="0", minute= "*", hour= "*")
You can find some more details about building the time string at.
For the purpose of our example, we will create a stateless session bean, which will act as
an automatic buying system and therefore, buy tickets at our ticketing store. So, we will
add one competitor in our quest for the best seat in the theatre! The following code snip-
pet explains this:
@Stateless
public class AutomaticSellerService {
private static final Logger logger =
Logger.getLogger(AutomaticSellerService.class);
@EJB