Jay SenSharma

Jay SenSharma

Here is a Simple demonstration of TimerManager & TimerListener … to perform any particular task at a regular interval of 10 Seconds (10*1000- Milliseconds).  In many cases we want to achieve this…feature sothat a Schedule task keep on working at the background at a regular interval..Like Updating Database…Cleaning some files & resources, Sending Server Statistics through E-Mail…etc.

In the following code all these kind of Scheduled activities you need to write inside “timerExpired(Timer timer)” method…which we need to implement as part of commonj.timers.TimerListener interface.

Timer scheduleAtFixedRate(TimerListener listener,
                          long delay,
                          long period)
                          throws IllegalArgumentException,IllegalStateException

Details Here:

Step1). Make a Directory inside your filesystem:    Example:   C:DEMO

Step2). Develop a WebApplication Directory inside C:DEMO with the name “TimerTestWar”.

Step3). Inside “C:DEMOTimerTestWAR” Provide “WEB-INF” directory.

Step4). Inside “C:DEMOTimerTestWARWEB-INFclasses” Please provide the “TimerServlet.java” as following…

package com.bea.test.timer;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import commonj.timers.Timer;
import commonj.timers.TimerListener;
import commonj.timers.TimerManager;

public class TimerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public static long prev=0L;
public static long currentSampleTime=0L;

public void service(HttpServletRequest req, HttpServletResponse res)
throws IOException {
PrintWriter out = res.getWriter();
try {
InitialContext ic = new InitialContext();
TimerManager tm = (TimerManager) ic.lookup("java:comp/env/tm/TimerManager");
tm.scheduleAtFixedRate(new TestTimerListener(), 0, 10 * 1000);
out.println("<h4>Timer scheduled! Check sysout</h4>");
} catch (NamingException ne) {
out.println("<h4>Timer schedule failed!</h4>");

private static class TestTimerListener implements TimerListener
public void timerExpired(Timer timer)
System.out.println("nnt inside timerExpired()...");
try {
System.out.print("ntt=====Write Uour actual Code here, Which u want to execute After Every 10-Second=========");    // Like Sending JVM Heap Utilization Report
System.out.println(""+new java.util.Date());
System.out.println("nt exiting timerExpired()...");
catch (Exception e)


Step5). Now provide the Servlet mapping inside “web.xml”  placed inside “C:DEMOTimerTestWARWEB-INF”

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

Step6). Now compile your Servlet :

C:DEMOTimerTestWARWEB-INFclasses>javac -d . TimerServlet.java

Step7). Now Provide a Simple “index.jsp” page inside your Timer Web Application …as following:

<h3> <a href="timerServlet">Click here</a>

Step8). Deploy your Application on WebLogic Server.



Output As soon as you hit the MyServlet….in the Server STDOUT you will see that after each 10-Second your Scheduled work started executing….as following:

Server Stdout




Jay SenSharma

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.