Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

What are the lifecycle methods for Timer service in EJB 2.1?

What are the lifecycle methods for Timer service in EJB 2.1?
The EJB timer service enables an EJB to be notified when an interval of time has arrived or elapsed. To use the timer service, an EJB must implement the javax.ejb.TimedObject interface, which defines a single callback method, ejbTimeout(). When the scheduled time period is reached, or the interval has elapsed, the EJB container invokes the EJB's ejbTimeout() method, as the following example illustrates:

TimerService timerService = ejbContext.getTimerService();
timerService.createTimer(someMilliseconds, "A test timer");
The lifecycle for a Timer object is as follows:

1) Creation - One of the createTimer methods of the TimerService interface is invoked.

2) Saving - Timers are saved when their container is shut down, when their container crashes, or when their TimerHandle is manually retrieved and saved.

3) Restoring - Timers are restored and become active again when their container is restarted or when their TimerHandle is the getTimer method is invoked.

4) Destroying - Timers are cancelled when the time interval expires, the EJB container cancels the timer (such as when the Timer's EJB instance is removed), or when the cancel method of the Timer is called.

The timer service itself is created by an application server usually during startup. During a graceful shutdown of the application server, the timer service is destroyed which will result in all timers being destroyed and triggers being cancelled.

Dig Deeper on Topics Archive

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.