EJB Timer Service

The EJB timer service lets developers schedule tasks for timed notifications. Since the Thread API cannot be used directly in the container environment, JEE provides the timer service as an alternative to threads. The developer registers an EJB for callbacks specifying a time expression. The time expression can be a specific date, some specific duration to elapse, or some interval for periodic notifications. The timer service can be used by singletons, stateless EJBs or message driven beans.

There are two options for creating timers: the @Schedule annotation and using the javax.ejb.TimerService interface. The timer service uses a UNIX cron-like expression to specify the date. The attributes are that can be used are second, minute, hour, dayOfMonth, month, dayOfWeek, year, and timezone. For example to create a timer to be fired every Monday and Tuesday at 10 seconds past noon:

@Schedule(second="10", hour="12", dayOfWeek="Mon, Tue”)
public void doSomething() {
//…
}

It is also possible to specify multiple timers to call the same method by using the @Schedules annotation. To create a timer to be called every day at 10 a.m. and every Friday at 11 a.m.:

@Schedules(
{
@Schedule(hour = "10"),
@Schedule(hour = "11", dayOfWeek = "Fri")
}
)
public void performSomething() { // ... }

The second option is to programmatically create a timer with the javax.ejb.TimerService interface. A reference to the javax.ejb.TimerService interface can be obtained with dependency injection, using EJBContext.getTimerService() or performing a JNDI lookup. The API has several methods for creating timers. Please see [1,2] for the available methods in the TimerService class. To use the TimerService by dependency injection in the EJB we specify:

@Resource
TimerService timerService;

Then to create a timer to be fired only once:

TimerConfig cnf = new TimerConfig(dataToProcess, true);
timerService.createSingleActionTimer(seconds * 1000, cnf);

TimerConfig specifies that the dataToProcess need to be passed to the timer method specified by the @Timeout annotation, and the true flag in the second argument specifies that the timer should be persistent. At the specified instant the container calls the method annotated with the @Timeout annotation:

@Timeout
public void timerExpired(Timer timer) {
Float dataToProcess = (Float) timer.getInfo();
logger.info("processing " + dataToProcess);
}

You can download the example here. The example has been tested on GlassFish v3.

References:
[1] http://javadoc.glassfish.org/javaee6/apidoc/javax/ejb/TimerService.html
[2] Beginning Java EE 6 Platform with GlassFish 3 by Antonio Goncalves

Tags: , , , ,

RSS 2.0 feed. Reply to post, or trackback.

Post comments

  1. Mortoza says:

    What version of EJB Timer Service you are talking about, 3.1? Which version of weblogic makes this Cron type Timer available?

  2. Raphael Parree says:

    This is indeed an article on the new features in EJB 3.1. When this will be available in WLS? Not sure, hopefully this year; it looked like they were going to make EJB 3.1 available earlier, but it now seems it will be bundled with their Java EE 6 release. Please check with Oracle to get an official answer :)

Leave a Reply