Timer service

The EJB Timer Service provides methods to allow callbacks to be scheduled for time-based events. Stateless session beans and entity beans may call the timer API to register for timer event notifications. Notifications may be scheduled to occur at a specific time, after a specific elapsed duration, or at specific recurring intervals.

The EJB Timer Service is a coarse-grained timer notification service that is designed for use in the modeling of application-level processes. It is not intended for the modeling of real-time events. While timer durations are expressed in millisecond units, this is because the millisecond is the unit of time granularity used by the APIs of the J2SE platform. It is expected that most timed events will correspond to hours, days, or longer.

An enterprise bean accesses the timer service through its EJBContext interface. The timer service provides methods for the creation and cancellation of timers, as well as for locating the timers that are associated with a bean. A timer is created to schedule timed callbacks. The bean class of an enterprise bean that uses the timer service must implement the javax.ejb.TimedObject interface. This interface has a single method, the timer callback method, ejbTimeout. When the time specified at timer creation elapses, the container invokes the ejbTimeout method of the bean. A timer may be cancelled by a bean before its expiration. If a timer is cancelled, the ejbTimeout method is not called. A timer is cancelled by calling its cancel method.

Invocations of the methods to create and cancel timers and of the ejbTimeout method are typically made within a transaction. The timer service is intended for the modelling of long-lived business processes. Timers survive container crashes and the activation/passivation and load/store cycles of the enterprise beans that are registered with them.