ServletContextListener with Timer

In this section, you will learn how to use timer with ServletContextListener to run code snippet after every 1 minute immediate after the application run.


 

Ads

Tutorials   
Ads

ServletContextListener with Timer

In this section, you will learn how to use timer with ServletContextListener to run code snippet after every 1 minute immediate after the application run.

EXAMPLE

In this example, we will display a message on console after every 1 minute. This code will start executing immediately after the execution of the project.

Listener is used for listening to events in a web container. For example, when a context is initialized or destroyed or session is created or an attribute is placed in a session. To receive these events regularly listener  is configure into web.xml .

In the below code, we are using ServletContextListener with timer to run code after every 1 min. ServletContextListener  immediately starts after code execution. And the Timer class of java.util.Timer is used inside  contextInitialized() function, which runs the code after every 1 minute.

The code is given below  :

import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
* Application Lifecycle Listener implementation class MyServletContextListener
*
*/
public class MyServletContextListener implements ServletContextListener {

/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent arg0) {
ServletContext servletContext = arg0.getServletContext();
System.out.println("*********ServletContextListener started*********");

int delay = 1000;
Timer timer = new Timer();
//final Calendar calendar = Calendar.getInstance();
//System.out.println("Tweet at Time = " + calendar.getTime());
//calendar.add(Calendar.SECOND, -60);
timer.scheduleAtFixedRate(new TimerTask(){
public void run(){
System.out.println("Running this code every 1 minute....");
}//End of Run
},delay, 60000);
servletContext.setAttribute ("timer", timer);
}

/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent arg0) {
ServletContext servletContext = arg0.getServletContext();
// get our timer from the Context
Timer timer = (Timer)servletContext.getAttribute ("timer");

// cancel all pending tasks in the timers queue
if (timer != null)
timer.cancel();

// remove the timer from the servlet context
servletContext.removeAttribute ("timer");
System.out.println("ServletContextListener destroyed");

}
}

OUTPUT

*********ServletContextListener started*********
Mar 15, 2012 6:53:55 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-9090
Mar 15, 2012 6:53:55 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Mar 15, 2012 6:53:55 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/15 config=null
Mar 15, 2012 6:53:55 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 217 ms
Running this code every 1 minute....

Download Source Code

Advertisements

Ads
Share on Google+Share on Google+

ServletContextListener with Timer

Posted on: March 15, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: ServletContextListener with Timer  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
ritu
July 17, 2012
t

nice one

Ads

 

Ads