javax.ejb.TimedObject


To configure an existing EJB to be able to receive timer service callbacks, the EJB must implement the javax.ejb.TimedObject interface. This interface provides a single method, ejbTimeOut(), which is invoked by the timer service. The interface is implemented in the EJB implementation class and is not exposed in the home, remote, local home, local, and web service endpoint interfaces. The javax.ejb.TimedObject interface is shown in Listing 10-9.

Listing 10-9: The javax.ejb.TimedObject interface.

start example
 javax.ejb.TimedObject public interface javax.ejb.TimedObject {    public void ejbTimeout(javax.ejb.Timer timer); } 
end example

This interface is implemented by bean instances that wish to listen for timer events. The single method ejbTimeout is invoked by the container and is passed a timer object. The timer object will be covered in detail below.

The EJB container treats timers for entity, stateless session, and message-driven beans differently. For stateless session and message-driven beans, timers can be set on any bean instance. At the time of callback invocation, the container will invoke the ejbTimeOut() callback method on any available component from the method-ready pool. In this case, the timer is registered with the stateless session or message-driven bean type, as opposed to a physical or logical instance.

For entity beans, timers are registered to a specific primary key. Upon invocation of the timer, the container will load an entity bean with the specific primary key and invoke the ejbTimeout() callback method on that specific entity bean. If the entity bean is removed, then any registered timers associated with the respective primary key are also removed by the container.

javax.ejb.TimerService

The javax.ejb.TimerService is the factory interface that represents the TimerService available in the EJB 2.1 specification. The container provider is responsible for the implementation of the TimerService interface. Access to the TimerService is via the EJBContext object. Depending on the type of EJB being implemented, this will be via the javax.ejb.SessionContext, javax.ejb.EntityContext, or javax.ejb.MessageDrivenContext object. To obtain the timer service, the getTimerService() method is invoked on the EJBContext to return a handle to the TimerService. Timers can then be registered, canceled, and queried by the EJB component.

Timer createTimer()

The overloaded createTimer() methods allow the creation of a timer. The choice of which method to use will be dependent on the behavior of the timer that is required. At the time of creation and registration, extra parameters can be supplied to the timer, which are in turn accessible by the bean instance upon the callback invocation. A timer can be created to execute after a particular interval, after a particular interval with a recurring interval, at a particular time, and at a particular time with a recurring interval.

Collection getTimers()

This method returns a collection of timers currently registered with the timer service. The collection can be enumerated for the management of timer registrations.




Enterprise JavaBeans 2.1
Enterprise JavaBeans 2.1
ISBN: 1590590880
EAN: 2147483647
Year: 2006
Pages: 103

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net