Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g (10.1.3.5.0) Part Number E13981-01 |
|
|
View PDF |
You can configure the following types of enterprise beans to use a Java EE timer:
EJB 3.0 stateless session beans and message-driven beans.
EJB 2.1 stateless session beans, entity beans with container-managed persistence, entity beans with bean-managed persistence, and message-driven beans.
To configure an enterprise bean with a Java EE timer, do the following:
Acquire the javax.ejb.TimerService
in one of the following ways:
For an EJB 3.0 enterprise bean, use the @Resource
annotation, as Example 25-1 shows.
For an EJB 3.0 or EJB 2.1 enterprise bean, use EJBContext
or InitalContext
method getTimerService
, as Example 25-1 shows.
Use TimerService
method createTimer
to create the appropriate type of timer (see the javax.ejb.TimerService
API), as Example 25-1 and Example 25-1 show.
When you create a Timer
on an EJB 2.1 entity bean, the container invokes the timeout callback method of that particular entity bean instance identified by its primary key. Timers created for a particular entity bean are removed when the entity bean is removed.
When you create a Timer
on any other type of enterprise beans, the container invokes the timeout callback method on any instance of that type in the pooled state.
Implement a timeout callback method.
This method must not be static
or final
and must have the following signature:
void <METHOD>(Timer timer)
You can implement a timeout callback method in one of the following ways:
For an EJB 3.0 enterprise bean, annotate any bean method using the @Timeout
annotation, as Example 25-1 shows.
For an EJB 3.0 or EJB 2.1 enterprise bean, implement the javax.ejb.TimedObject
interface, as Example 25-1 shows.
Example 25-1 Configuring a Java EE Timer on an EJB 3.0 Stateless Session Bean
import javax.ejb.Stateless; import javax.ejb.TimerService; import javax.ejb.Timeout; import javax.ejb.Timer; import javax.ejb.TransactionAttribute; import javax.ejb.TransactionAttributeType; @Stateless; public class TimerServiceBean implement MyTimerService { // injection of TimerService @Resource TimerService timerService; // implement bean business interface MyTimerService @TransactionAttribute(value=TransactionAttributeType.REQUIRES_NEW) // default TransactionAttributeType.REQUIRED public void createTimer(Serializable timerInfo) { timerService.createTimer(timeout, info); } ... // user annotated timeout method @Timeout @TransactionAttribute(value=TransactionAttributeType.REQUIRES_NEW) public void timeoutCallback(Timer timer) { ... } }
Example 25-2 Configuring a Java EE Timer on an EJB 2.1 Stateless Session Bean
import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.EJBContext; import javax.ejb.EJBException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; import javax.ejb.TimedObject; import javax.ejb.Timer; import javax.ejb.TimerService; class ServiceBean_2_1 implements SessionBean, MyTimerService, TimedObject { EJBContext ctx; // implement bean business interface MyTimerService public void createTimer(long duration, Serializable info) { TimerService timerService = ctx.getTimerService(); timerService.createTimer(duration, info); } // implement TimedObject public void ejbTimeout(Timer timer) { System.out.println("Timeout: " + timer.getInfo()); } ... // implement SessionBean public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException { this.ctx = ctx; } }