Implementation
This section describes how the FBS 10g uses the EJB container-managed
timer service with stateless session beans.
The javax.ejb package provides four interfaces
that are related to timers:
- TimedObject
- Timer
- TimerHandle
- TimerService
When a timer expires (goes off), the EJB container calls
the ejbTimeout method of the bean's implementation class. The ejbTimeout
method is a callback that contains the business logic that handles the timed
event. Because ejbTimeout is defined by the javax.ejb.TimedObject
interface, the bean class must implement TimedObject.
The UserManagementSessionFacadeBean implements
the TimedObject interface. It also implements the ejbTimeout
which is invoked when the timer goes off. An instance of TimerService
is obtained from the EJBContext in the initializeTimer
method and then createTimer is called on the Timer Service to set
the required timer interval.
The TimerHandle for the timer is serialized in the bean and
used to obtain the details of the timer for cancellation or changing the interval.
Once the timer goes off, the bean queries the trade details that happened during
the previous timeout and the current time, composes a mail with the trade details,
and sends it to the admin email ID (admin email can be configured in Connection.properties).
The following code from UserManagementSessionFacadeBean.initializeTimer
gets a TimerService instance and uses it to create a timer with
a specified interval.
private void initializeTimer(long timeout, String info) { try { // Get an instance of TimerService associated with the // SessionContext of the SLSB TimerService ts = sctx.getTimerService(); // If there are existing Timer associated with this Bean, // Cancel it before initializing another one checkAndCancelTimers(ts.getTimers()); long initialDuration = timeout; long intervalDuration = timeout; // Create Timer with required interval // The timeout value is also set in the infor field for convenience Timer timer = ts.createTimer(initialDuration, intervalDuration, info + " Timeout : " + timeout);
// Store the TimerHandle which is seriablizable and which can be used // to retrieve the timer values whenever required later.
// Class-level attribute: private TimerHandle timerHandle = null; timerHandle = timer.getHandle(); System.out.println("Timer created at " + new Date(System.currentTimeMillis()) + " with a timeout: " + intervalDuration + " ms and with info: " + info); } catch (Exception ex) { ex.printStackTrace(); } return; }
The code below comes from UserManagementSessionFacadeBean.ejbTimeout,
used by the EJB container to deliver timer expiration notifications. It implements
the callback method defined on the TimedObject interface. This
method contains all the business logic to send timed reports of the trading
activity to the FBS Administrator.
Note: This method must be implemented by an entity bean or stateless session
bean or message-driven bean class which implements the TimedObject interface
to process the timer expirations.
public void ejbTimeout(Timer timer) {
Date sysDate = new Date(System.currentTimeMillis());
Date toDate = new Date(System.currentTimeMillis() - timer.getTimeRemaining());
// Get the timer information
String timerInfo = (String) timer.getInfo();
System.out.println("ejbTimeout() called at: " +
sysDate + " with info: " + timerInfo);
// Call the method to send mail containing information of the
// trading activity on the FBS site to the administrator
sendMail(timerInfo, toDate, sysDate);
return;
}
The UI for setting a timer interval is provided by ReportInterval.jsp.
|