Message-Driven Bean Timers
Message-driven bean timers are similar to stateless session bean timers in several ways. Timers are associated only with the type of bean. When a timer expires, a message-driven bean instance is selected from a pool to execute the timeout callback method. In addition, message-driven beans can be used for performing audits or other types of batch jobs. The primary difference between a message-driven bean timer and a stateless session bean timer is the way in which they’re initiated: timers are created in response to an incoming message or, if the container supports it, from a configuration file.
In order to initialize a message-driven bean timer from an incoming
message, you simply put the call to the TimerService.createTimer() method in the
message-handling method. For a JMS-based message-driven bean, the method
call goes in the onMessage() method:
@MessageDriven
public class JmsTimerBean implements MessageListener {
@Resource TimerService timerService
public void onMessage(Message message){
MapMessage mapMessage = (MapMessage)message;
long expirationDate = mapMessage.getLong("expirationDate");
timerService.createTimer(expirationDate, null );
}
@Timeout
public void timeout( ){
// put timeout logic here
}The incoming JMS message should contain information about the timer:
the beginning (start) date, duration, or even the Serializable info object. Combining JMS with the Timer Service can offer some powerful design options for implementing audits, batch processing, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access