O'Reilly logo

Enterprise JavaBeans, Fourth Edition by Richard Monson-Haefel, Bill Burke, Sacha Labourey

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required