Home > EJB 3.1 > EJB 3.1 and TimerService challenges Quartz Framework

EJB 3.1 and TimerService challenges Quartz Framework

A significant number of enterprise applications have some sort of time-driven requirements. For a long time the spec has ignored such needs, forcing developers to find non-standard solutions like Quartz or Flux. EJB 2.1 introduced the Timer Service, consisting of a service provided by the container that allows EJBs to have timer callbacks being invoked at specified times. Moreover, such invocations can be done in a transactional context.

Even though the Timer Service was able to bridge some of the necessities, there were considerable limitations, like for example:

  • All timers have to be created programatically.
  • Lack of flexibility in the scheduling of timers.
  • Missing support for the use of timers in environments with multiple JVMs like clustering.

With EJB 3.1 there are two ways to create timers:

  • Programatically, using the already existing TimerService interfaces. This interface has been greatly enhanced in order to provide more flexibility while creating timers.
  • Declaratively, using annotations or the deployment descriptor. This way a timer can be statically defined so it is automatically created during application startup.

A method can be annotated with more than one timer, like shown below, where two timers are defined for the method mealTime, one which will expire every day at 1pm and another expiring at 8pm.

public class MealEJB { 

    {    @Schedule(hour="13"),
    public void mealTime(Timer timer) { (...) }


In terms of timer scheduling there were major improvements. The callback schedule can be expressed using a calendar-based syntax modeled after the UNIX cron. There are eight attributes which can be used in such expressions:

Attribute Allowable Values Example
second [0, 59] second = “10”
minute [0, 59] minute = “30”
hour [0, 23] hour = “10”
dayOfMonth – [1, 31] – day of the month 

– Last – last day of the month

– -[1, 7] – number of days before end of month

– {“1st”, “2nd”, “3rd”, “4th”, “5th”, …, “Last”} {“Sun”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”}- identifies a single occurrence of a day of the month

dayOfMonth = “3” 

dayOfMonth = “Last”

dayOfMonth = “-5”

dayOfMonth = “1st Tue”

month – [1, 12] – month of the year 

– {“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”}- month name

month = “7” 

month = “Jan”

dayOfWeek – [0, 7]- day of the week where both 0 and 7 refer to Sunday 

– {“Sun”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”}- day’s name

dayOfWeek = “5” 

dayOfWeek = “Wed”

year Four digit calendar year year = “1978”
timezone Id of the related timezone timezone = “America/New_York”

The values provided for each attribute can be expressed in different forms
Note any of the attributes support the cron-style “*” wildcard to represent all values, a comma separated list (such as “Jan, Feb, Mar” for the month attribute) or a dash-separated range (such as “Mon-Fri” for the day of week attribute).

There are two different types of @Schedule:

  1. Persistent – specified by persistent=true in @Schedule annotation
    This is the default type of any @Schedule.
  2. Non-persistent – specified by persistene=false in @Schedule annotation

Both persistent and non-persistent timers can exist simultaneously, and the persistent and non-persistent configurations are not mutually exclusive. Your application might use both persistent and non-persistent timers.

Use persistent timers when the timer must persist through server shutdowns and restarts. Otherwise, use non-persistent timers when a server shutdown must cancel the timer.

When a persistent timer does not fire because the server is unavailable, then the missed attempt is recovered when the server restarts. When a non-persistent timer does not fire because the server is unavailable, the missed attempt is not recovered, because the server shutdown cancels the non-persistent timer.


O'Reilly Enterprise JavaBeans 3.1
Categories: EJB 3.1
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: