EJB3 Timer Service TimerConfig Example

21 November 2012 By Nithya Vasudevan 5,787 views No Comment
9 Flares Twitter 1 Facebook 0 Google+ 8 9 Flares ×

Environment Used

  • JDK 6 (Java SE 6)
  • EJB 3.x Stateless Session Bean
  • Eclipse Indigo IDE for Java EE Developers
  • JBoss Tools – for Eclipse Indigo
  • JBoss Application Server (AS) 7.1.0 Final

Setting up development environment:
Read this page for installing and setting up the environment for developing and deploying EJB 3.x on JBoss application server.

Project Description

  • This EJB3 Timer tutorial explains how to create EJB3 timer service with TimerConfig in stateless session bean.
  • Client invokes a method which creates a timer and when timer expires the container invokes the timeout method.
  • This example is deployed in JBoss application server.
  • For testing this example we create a remote Java Application Client (main()) which is created in the same project as session bean.

Project Folder Structure

The figure below shows the final directory structure of this example.

Bean Business Interface

Create business interface “TimerRemote” in package “com.theopentutorials.ejb3.business” and copy the following code.

package com.theopentutorials.ejb3.business;
import javax.ejb.Remote;

public interface TimerRemote {
	public String checkTimerStatus();
	public void startTimer();

Bean Implementation Class

Create the bean implementation class “TimerBean” in package “com.theopentutorials.ejb3.businesslogic” and copy the following code.

package com.theopentutorials.ejb3.businesslogic;

import java.util.Collection;
import java.util.Iterator;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerConfig;
import javax.ejb.TimerService;
import com.theopentutorials.ejb3.business.TimerRemote;

public class TimerBean implements TimerRemote {

	TimerService service;

	public void startTimer() {		
		TimerConfig config = new TimerConfig();
		Timer timer = service.createSingleActionTimer(2000, config);
		//Timer timer = service.createIntervalTimer(2000, 2000, config);
		System.out.println("Timers set");

	public void handleTimeout(Timer timer) {
		System.out.println("Handle timeout event here...");

	public String checkTimerStatus() {
		Timer timer = null;
		Collection<Timer> timers = service.getTimers();
		Iterator<Timer> iterator = timers.iterator();
		while (iterator.hasNext()) {
			timer = iterator.next();
			return ("Timer will expire after " + 
				timer.getTimeRemaining() + " milliseconds.");
		return ("No timer found");
  • TimerConfig is used to specify additional timer configuration settings during timer creation.
  • The persistent property determines whether the corresponding timer has a lifetime that spans the JVM in which it was created. It is optional and defaults to true.

These methods of TimerService takes timer config as parameter and creates timer.
Since EJB3.1

Timer createIntervalTimer(java.util.Date initialExpiration, long intervalDuration, TimerConfig timerConfig)
Timer createIntervalTimer(long initialDuration, long intervalDuration, TimerConfig timerConfig)
Timer createSingleActionTimer(java.util.Date expiration, TimerConfig timerConfig)
Timer createSingleActionTimer(long duration, TimerConfig timerConfig)

Java Application Client

Client Utility class (JNDILookupClass) for JBoss AS 7.1

Create client utility class “JNDILookupClass” in package “com.theopentutorials.ejb3.clientutility” and copy the following code.

package com.theopentutorials.ejb3.clientutility;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JNDILookupClass {

	private static Context initialContext;
	private static final String PKG_INTERFACES = "org.jboss.ejb.client.naming";

	public static Context getInitialContext() throws NamingException {
		if (initialContext == null) {
			Properties properties = new Properties();
			properties.put(Context.URL_PKG_PREFIXES, PKG_INTERFACES);

			initialContext = new InitialContext(properties);
		return initialContext;


Create client class “EJBApplicationClient” in package “com.theopentutorials.ejb3.client” and copy the following code.

package com.theopentutorials.ejb3.client;

import javax.naming.Context;
import javax.naming.NamingException;
import com.theopentutorials.ejb3.business.TimerRemote;
import com.theopentutorials.ejb3.businesslogic.TimerBean;
import com.theopentutorials.ejb3.clientutility.JNDILookupClass;

public class EJBApplicationClient {
	public static void main(String[] args) {
		// 1. Creating bean instance through lookup
		TimerRemote bean = doLookup();
		// 5. Call bean methods
		//starts the timer

	private static TimerRemote doLookup() {
		Context context = null;
		TimerRemote bean = null;
		try {
			// 2. Obtaining Context
			context = JNDILookupClass.getInitialContext();
			// 3. Generate JNDI Lookup name
			String lookupName = getLookupName();
			// 4. Lookup and cast
			bean = (TimerRemote) context.lookup(lookupName);

		} catch (NamingException e) {
		return bean;

	private static String getLookupName() {
		 * The app name is the EAR name of the deployed EJB without .ear suffix.
		 * Since we haven't deployed the application as a .ear, the app name for
		 * us will be an empty string
		String appName = "";

		// The module name is the JAR name of the deployed EJB without the .jar
		// suffix.
		String moduleName = "EJB3TimerService";

		 * AS7 allows each deployment to have an (optional) distinct name. This
		 * can be an empty string if distinct name is not specified.
		String distinctName = "";

		// The EJB bean implementation class name
		String beanName = TimerBean.class.getSimpleName();

		// Fully qualified remote interface name
		final String interfaceName = TimerRemote.class.getName();

		// Create a look up string name
		String name = "ejb:" + appName + "/" + moduleName + "/" + distinctName
				+ "/" + beanName + "!" + interfaceName;

		return name;

We use JBoss AS 7.1 and EJB3.1 look up naming convention.

If the application server uses EJB3 look up naming convention, (JBoss AS 5 or 6) then the following lookup name should be used.

JBoss console output

[stdout] Timers set
[stdout] Handle timeout event here…

For a step by step tutorial on creating and deploying EJB project in
JBoss AS 7 -> refer this page.
JBoss AS 6 -> refer this page.
JBoss AS 5 -> refer this page.

Tags: , , , ,