Skip Headers

Oracle® Application Server Containers for J2EE Enterprise JavaBeans Developer's Guide
10g Release 2 (10.1.2)
Part No. B15505-01
  Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Previous
Previous
Next
Next
 

How to Create Timers

The EJB that creates the timer first retrieves the timer service—TimerService interface—through the getTimerService method of the EJBContext interface. From the TimerService interface, you can create a timer using one of the four provided createTimer methods that allow you to specify the timer as a single-event timer or as an interval timer. The timers are defined in milliseconds, even though most events are for much longer time periods. The expiration of the timer can be defined as a duration or in absolute time. In addition, the bean can pass some information to identify the timer, which must be serializable.

TimerService ts = ctx.getTimerService();Timer myTimer = ts.createTimer(timeout, "EmpDurationTimer");

The timer is created by a bean to designate when a callback method is invoked. The business logic that is to be executed when the timer expires is implemented in a callback method—ejbTimeout—within the application bean class. The bean class that uses the timer service must implement the javax.ejb.TimedObject interface, which contains the ejbTimeout method.

The created timer is associated with the identity of the bean. For entity beans, the ejbTimeout is invoked on the bean instance that created the bean; for stateless session beans and MDBs, the ejbTimeout method is invoked on any bean instance in the pool.

public abstract class EmployeeBean implements EntityBean, TimedObject
...
  public void ejbTimeout(Timer timer)  {    System.out.println("ejbTimeout() called at: " + new            Date(System.currentTimeMillis()) + " with info: " + timer.getInfo());    return;  }


Note:

There is no guarantee that the timers are executed in any order; therefore, your implmentation within the ejbTimeout callback must be able to handle the callbacks in any sequence.

The TimerService provides the following methods for creating the different types of timers:

public interface javax.ejb.TimerService {
  /* After a specified duration*/
  public Timer createTimer(long duration, java.io.Serializable info);
 /* At a specifed interval */
  public Timer createTimer(long initialDuration, long intervalDuration,                                java.io.Serializable info);
  /* At a certain time */
  public Timer createTimer(java.util.Date expiration, java.io.Serializable info);
  /*  A certain duration after a specified date and time */
  public Timer createTimer(java.util.Date initialExpiration,                              long intervalDuration, java.io.Serializable info);  public Collection getTimers();}

The getTimers method retrieves all active timers associated with the bean.


Note:

Timers and their handles are local objects; therefore, they should not be passed through the bean remote interface.