Home JBoss Server EJB Timer Service
30 | 07 | 2010
JBoss 5 AS Book
"JBoss AS 5 development" reviews
Please share your feedback/review with other readers!
Banner
Dashboard
Advertise with Us
Banner
RSS Feed
Java EE 1.6 resources
Login
Sign here for the NewsLetter.



JBoss admin resources
Banner
Java EE 1.6 resources
JBoss howto

How to avoid the 50 seconds start up limit in Eclipse ?

JBoss recipe of the day ...
Read More

How can you solve deployment errors caused by large war/jar/ear files ?

jboss recipe of the day ...
Read More

How do you configure your .war to be deployed after your EJB ?

jboss recipe of the day ...
Read More
EJB Timer Service PDF Print E-mail
Written by F.Marchioni   
Monday, 23 November 2009 13:58
The Timer Service can be used to build applications that depends on time based services. In this tutorial we will show how you can receive timer notifications on your Stateless Session Beans.
You can implement Timers in two ways:
 
  • Implementing the javax.ejb.TimedObject in your SLSB
  • Adding a method with the @Timeout annotation in your SLSB
     

We will show the latter approach:
Create a new Stateless Bean named TimerSampleBean and its remote interface TimerSample
package com.sample;

import java.util.Date;
import javax.annotation.Resource;
import javax.ejb.*;

@Stateless

public class TimerSampleBean implements TimerSample
{
   private @Resource SessionContext ctx;

   public void scheduleTimer(long milliseconds)
   {
      ctx.getTimerService().createTimer(new Date(new Date().getTime() + milliseconds), "Hello World");
   }

   @Timeout
   public void timeoutHandler(Timer timer)
   {
      System.out.println("---------------------");
      System.out.println("* Received Timer event: " + timer.getInfo());
      System.out.println("---------------------");

      timer.cancel();
   }
}


package com.sample;

import javax.ejb.Remote;

@Remote
public interface TimerSample {
	void scheduleTimer(long milliseconds);

}


As you can see, the timer is scheduled in the schedulerTimer method :
 
public void scheduleTimer(long milliseconds)  {
      ctx.getTimerService().createTimer(new Date(new Date().getTime() + milliseconds), "Hello World");
}

An enterprise bean schedules itself for a timed notification using a reference to the TimerService , which can be obtained from the EJBContext or injected directly into your bean using the @javax.annotation.Resource annotation. The TimerService allows a bean to register itself for notification on a specific date, after some period of time, or at recurring intervals.
 
The notification is handled in the timeoutHandler method which receives as argument an instance of the Timer object:

@Timeout
   public void timeoutHandler(Timer timer)
   {
      System.out.println("---------------------");
      System.out.println("* Received Timer event: " + timer.getInfo());
      System.out.println("---------------------");
      timer.cancel();
   }

As you can see, after the notification has been acquired, the timer is cancelled, otherwise the notification would repeat at regual intervals.

Building the Client

Building a Test client doesn't require anything else then looking up the SLSB and invoking the method scheduleTimer which schedules the timeout handler after 5000 ms:
 
import java.util.Hashtable;
import javax.naming.InitialContext;

public class TestClient {
	public static void main(String[] args) throws Exception
	   {
		Hashtable ht=new Hashtable();   
		ht.put(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");   
		ht.put(InitialContext.PROVIDER_URL,"jnp://localhost:1099");   
		ht.put(InitialContext.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
		 

		InitialContext ctx = new InitialContext(ht);
	     
	      TimerSample timer = (TimerSample) ctx.lookup("TimerSampleBean/remote");
	      timer.scheduleTimer(5000);
	   }

}


Single actions timers and Interval timers

In the above example we are scheduling a timer after 5000 ms. In such a scenario the timer would repeat its scheduling until the cancel method is called.
You can, however, schedule a timer for a single action. For his purpose you have two distinct createTimer methods:

createTimer(Date expiration, Serializable info)

Creates a single-action timer that expires once. The timer expires on the date set for the expiration parameter. Here's how to set a timer that expires on November 4, 2009:
 
Calendar calender = Calendar.getInstance( );
calendar.set(2009, Calendar.NOVEMBER, 4);
timerService.createTimer(calendar.getTime( ), null);
 
 
createTimer(long duration, Serializable info)

Creates a single-action timer that expires only once. The timer expires after duration time (measured in milliseconds) has elapsed. Here's how to set a timer that expires in 30 days:
 
long thirthyDays = 1000 * 60 * 60 * 24 * 30; // 30 days
timerService.createTimer(thirthyDays, null);
 
Developers can take the advantage of the EJB Timer Services for building robust scheduling applications. If you however need a more complex strategy for your Enterpirse applications I would suggest having a look at the Quartz framework:

http://www.mastertheboss.com/en/jboss-application-server/84-jboss-quartz.html

JBoss.org Search
Custom Search
Comments
Search
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated on Monday, 23 November 2009 15:21