Timer Service API

The Timer Service enables an enterprise bean to be notified when a specific date has arrived, when some period of time has elapsed, or at recurring intervals. To use the Timer Service, an enterprise bean must implement the javax.ejb.TimedObject interface, which defines a single callback method, ejbTimeout( ):

package javax.ejb;

public interface TimedObject {
    public void ejbTimeout(Timer timer) ;
}

When the scheduled time is reached or the specified interval has elapsed, the container system invokes the enterprise bean’s ejbTimeout( ) method. The enterprise bean can then perform any processing it needs to respond to the timeout, such as run reports, audit records, modify the states of other beans, etc. For example, the Ship EJB can be modified to implement the TimedObject interface, as shown:

public abstract class ShipBean 
      implements javax.ejb.EntityBean, javax.ejb.TimedObject {
    javax.ejb.EntityContext ejbContext;
    public void setEntityContext(javax.ejb.EntityContext ctxt){
         ejbContext = ctxt;
    }
    public void ejbTimeout(javax.ejb.Timer timer) {
               // business logic for timer goes here
               } public Integer ejbCreate(Integer primaryKey,String name,double tonnage) { setId(primaryKey); setName(name); setTonnage(tonnage); return null; } public void ejbPostCreate(Integer primaryKey,String name,double tonnage) {} public abstract void setId(Integer id); public abstract Integer getId( ); public abstract void setName(String name); public abstract String getName( ); public abstract void ...

Get Enterprise JavaBeans, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.