Java EE in the trenches - How-to use the TimerService and write a timer session bean
After writing a book about it and a long series of posts, finally I’m having the opportunity (thanks to sourcesense, obviously) to work on a real, full-stack Java EE project (with Glassfish); I know it might seem a bit ironic, but it’s a fact that customers aren’t usually too eager to adopt new technologies.
Anyway, the overall impression by now is very positive. In less than four days I’ve been able to sketch a prototype of an integration project and, most important, to learn other interesting features of the platform. In this new series of post I’ll try to share some tricks, tips and how-to I’m learning on the way.
How-to use the TimerService and write a timer session bean
Every Java EE 5 ejb container has a timer service. As the Java EE tutorial says, “the timer service of the enterprise bean container enables you to schedule timed notifications for all types of enterprise beans except for stateful session beans. You can schedule a timed notification to occur at a specific time, after a duration of time, or at timed intervals”.
From an EJB you can access the TimerService simply using the javax.annotation.Resource annotation; the timerService.createTimer(…) method initializes the timer service, while every Enterprise Java Bean (except for Stateful EJBs) can catch the timeout event simply using the javax.ejb.Timeout annotation. Let’s write a TimerSessionBean:
@Stateless
public class TimerSessionBean implements TimerSession {
@Resource private TimerService timerService;
public void init() {
timerService.createTimer(50, 10000, "My first timer service");
}
@Timeout
public void process(Timer timer) {
//... do something
}
}
with it’s local inteface
@Local
public interface TimerSession
{
public void init();
public void process(Timer timer);
}
If, for instance, you want to setup the TimerService at your application startup, you can write a ServletContextListener like this one:
public class StartupContextListener implements ServletContextListener
{
@EJB private TimerSession timerSession;
public void contextInitialized(ServletContextEvent arg0) {
timerSession.init();
}
public void contextDestroyed(ServletContextEvent arg0) {
}
}
Pack the StartupContextListener in a war file, with a web.xml descriptor similar to this one:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="2.5"
xmlns="http://java.sun. com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Your web app</display>
<listener>
<listener-class>it.pronetics.kafka.producer.servlet.StartupContextListener</listener-class>
</listener>
</web>
..and you’ve successfully instantiated your TimerService which, after an initial delay of 50 milliseconds, sends a timeout event every 10000 milliseconds. Note that there are several different ways to setup the TimerService: check its Javadoc page for further details.
Now the process method of the TimerSession stateless session bean will be invoked every time the TimerService expires.

3 Comments