Monday, November 25, 2013

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.

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.Timeris 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....

4 comments:

oakleyses said...

louis vuitton handbags, oakley sunglasses, louboutin, longchamp outlet, nike shoes, louis vuitton outlet stores, chanel handbags, burberry outlet, prada outlet, jordan shoes, tiffany and co, michael kors outlet, tory burch outlet, louis vuitton outlet, longchamp handbags, nike free, true religion jeans, michael kors outlet, kate spade outlet, polo ralph lauren outlet, tiffany and co, prada handbags, polo ralph lauren outlet, michael kors outlet, michael kors outlet, longchamp handbags, oakley sunglasses, ray ban sunglasses, kate spade handbags, burberry outlet, louis vuitton outlet, louboutin outlet, louboutin, coach factory outlet, air max, air max, coach outlet, gucci outlet, christian louboutin shoes, michael kors outlet, coach purses, ray ban sunglasses, michael kors outlet, louis vuitton, coach outlet store online, true religion jeans, oakley sunglasses cheap

oakleyses said...

louis vuitton handbags, oakley sunglasses, louboutin, longchamp outlet, nike shoes, louis vuitton outlet stores, chanel handbags, burberry outlet, prada outlet, jordan shoes, tiffany and co, michael kors outlet, tory burch outlet, louis vuitton outlet, longchamp handbags, nike free, true religion jeans, michael kors outlet, kate spade outlet, polo ralph lauren outlet, tiffany and co, prada handbags, polo ralph lauren outlet, michael kors outlet, michael kors outlet, longchamp handbags, oakley sunglasses, ray ban sunglasses, kate spade handbags, burberry outlet, louis vuitton outlet, louboutin outlet, louboutin, coach factory outlet, air max, air max, coach outlet, gucci outlet, christian louboutin shoes, michael kors outlet, coach purses, ray ban sunglasses, michael kors outlet, louis vuitton, coach outlet store online, true religion jeans, oakley sunglasses cheap

oakleyses said...

mac cosmetics, mont blanc, marc jacobs, canada goose outlet, nike huarache, vans shoes, soccer jerseys, hollister, giuseppe zanotti, beats by dre, abercrombie and fitch, longchamp, insanity workout, celine handbags, bottega veneta, ghd, nfl jerseys, north face outlet, chi flat iron, ugg boots, birkin bag, ugg australia, canada goose, herve leger, ugg pas cher, rolex watches, valentino shoes, canada goose uk, canada goose, ferragamo shoes, canada goose, ugg boots, uggs outlet, north face jackets, soccer shoes, asics running shoes, new balance shoes, p90x, lululemon outlet, canada goose jackets, mcm handbags, instyler, babyliss pro, ugg, wedding dresses, jimmy choo outlet, reebok outlet, nike roshe run

oakleyses said...

parajumpers, karen millen, air max, converse, pandora charms, moncler, louboutin, moncler, links of london, lancel, juicy couture outlet, oakley, hollister, pandora charms, supra shoes, thomas sabo, canada goose, gucci, wedding dresses, timberland boots, swarovski crystal, air max, coach outlet store online, moncler, ray ban, canada goose, moncler, ugg, louis vuitton, swarovski, hollister, montre homme, moncler, hollister clothing store, ralph lauren, rolex watches, moncler outlet, moncler, iphone 6 cases, baseball bats, juicy couture outlet, toms shoes, vans, pandora jewelry, ugg, converse shoes