Travelling, and not Arriving

          ... a good traveler has no fixed plans and is not intent upon arriving... (Lao Tzu)

Posted
30 October 2006 @ 9pm

Tagged
Java, Open Source, TechArticles

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

Posted by
Kenneth Saks
4 December 2006 @ 6pm

The EJB Timer Service API
is only accessible within the ejb tier.
Web components would need to delegate
to an ejb component to perform any
ejb timer operations.


Posted by
filippo
5 December 2006 @ 9am

You are right Kenneth, thank you for your comment. I’ve upadated the post.


Posted by
Rodrigo
24 July 2007 @ 5pm

Hi. I was trying your example but for some reason the timerSession in the StartupContextListener is always null. I think injection is not working. Any idea what could be wrong?

Thanx


Leave a Comment

The “From J2ee to Java EE Tour” on Google Video JugMilano’s November meeting next Tuesday