Timer Service in Enterprise Java Bean

The Timer Service is in build feature of any Application Server. It is used to develop time based enterprise application like; Shopping Cart in which if user does dot intract with Stateful Session Bean certain time EJB Container will ask password once again for conformation whether user is valid or not.

Some facts of Timer Service
  • It is only possible in Session Bean.
  • The EJB Container checks and call @Timeout annotated method.
  • User can start and stop timer.
  • This Service is also available in previous version of EJB
In this example, I am explaing how to create Timer service jn EJB

Below steps are to create Stateful Session Bean :

  • Create Enterprise Application StatelessTimer.
  • Create Stateless Session Bean with Remote and Local in EJB project
  • Add EJB Project in Web Project
  • Create index.jsp in Web project
  • Create Servlet in Web project
  • Run the Application

Step 1: Create Enterprise Application Project StatelessTimer.ear with EJB (StatelessTimerEJB)and Web (StatelessTimerWeb) Module
Click here if you are not familiear how to create Enterprise Application Project

Step 2: Create Stateless Session Bean in EJB project
Right click on EJB project -> New -> Session Bean (EJB3.X) and specify bean class name SessionTimer
This step will create two java file. One local interface SessionTimerLocal.java and bean class SessionTimer.java.

package techmyguru.timerservice;
import javax.ejb.Local;
public interface SessionTimerLocal {
  public void startTimer(String user);
  public String checkStatus();
  public String stopTimer();

package techmyguru.timerservice;
// all import statements
public class SessionTimer implements SessionTimerLocal 
private SessionContext ctx;

public void handleTimeout(Timer timer
System.out.println("Timeout handler called.");

public void startTimer(String user
ctx.getTimerService().createTimer(10000new Data(user));
System.out.println("Timers set");

public String checkStatus() 
Timer timer = null;
Collection timers = ctx.getTimerService().getTimers();
Iterator iter = timers.iterator();
while (iter.hasNext()) 
timer = (Timeriter.next();
Data data = (Datatimer.getInfo();
return ("Timer will expire after " + timer.getTimeRemaining() 
  " milliseconds.");
return ("No timer found");

public String stopTimer() 
String message = "";
Timer timer1 = null;
Collection timers = ctx.getTimerService().getTimers();
if (timers.size() != 0
Iterator iter = timers.iterator();
while (iter.hasNext()) 
timer1 = (Timeriter.next();
message = "Timer stoped";

message = "Timer not found";
return message;

package techmyguru.timerservice;
import java.io.Serializable;
public class Data implements Serializable{
  String message = "default message";
  public Data() {    }

  Data(String str1) {
    this.message = str1;

  public String getMessage() {
    return message;

  public void setMessage(String message) {
    this.message = message;

Step 3: Add EJB Project to Web project
Right Click on Web Project - > Properties - > Java Build Path -> Click Projects -> Add -> Select StatelessTimerEJB.

Step 4: Create index.jsp in Web project
<title>Insert title here</title>
<h2>Timer Service in EJB </h2>
<form action="Servlet1">
Enter User name<input type="text" name="user"
<input type="Submit" value="Start timer">
<input type="hidden" name="task" value="createTimer">
<a href="Servlet1?task=statusTimer">Timer Status</a></br>
<a href="Servlet1?task=stopTimer">Stop Timer</a></br>

Step 5: Create Servlets in Web project to use Stateless session bean
package techmyguru.web;
// all imports
public class Servlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;

SessionTimerLocal local;

protected void doGet(HttpServletRequest request, HttpServletResponse response
    throws ServletException, IOException 
  PrintWriter pw=response.getWriter();
  String task=request.getParameter("task");
    pw.println("Timer started");
  }else if(task.trim().equals("statusTimer"))
    pw.println("Timer Status - "+local.checkStatus());
  }else if(task.trim().equals("stopTimer"))
    pw.println("Timer stoped");
  pw.println("<br><a href='index.jsp'>Try Again</a></h2>");

Step 6: Verify the final directory structure of the project
Timer in EJB

Step 7: Run the Application
Right click on Web Project -> Run As -> Run on Server
Timer in EJB
Input user name and click Start Timer and get below screen.

Timer in EJB

You will get message on Server console
Timer in EJB

If timer has not started and user clicks Stop Timer then you will get below output.
Timer in EJB

Previous Download Source Code Next

Comment by : Olaitan October 3rd, 2015
Comment : An application wtterin on VB6 using CAS-COM bridge working fine in Windows XP, but not running in Win 7, is there any way out, please help.Thanks,Kamalesh.

Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code