Using servlet container to implement program monitoring in Java

Source: Internet
Author: User
Tags date final implement log min string
Servlet| Program


Take two steps: (1). Two methods for implementing the Javax.servlet.ServletContextListener Interface: contextinitialized () and contextdestroyed () contextinitialized () : When the servlet container starts, it executes contextdestroyed (): Executes when the servlet container stops (2). Add a program in contextinitialized () that requires listening, and the Java.util.Timer schedule () method to control how often the listener executes

DEMO (This is one of my SMS reply to listen to the program prototype, streamlined a bit)

----------------------------------------------------------------Replylistener.java--------------------------------------- -------------------------

Package com.hanweb.jcms;

Import javax.servlet.*;

public class Replylistener implements Servletcontextlistener {private Replytimer RT = NULL;    public void contextinitialized (Servletcontextevent event) {String status = ' [SYS] SMS reply listener start. '    Event.getservletcontext (). log (status); SYSTEM.OUT.PRINTLN (status);

RT = new Replytimer (1);  Rt.start (); }

public void contextdestroyed (Servletcontextevent event) {String status = [SYS] SMS reply listener stop. "    Event.getservletcontext (). log (status); SYSTEM.OUT.PRINTLN (status);

if (RT!= null) {rt.stop (); }  }}

----------------------------------------------------------------Replytimer.java------------------------------------------ ----------------------

Package com.hanweb.jcms;

Import java.util.*;

public class Replytimer {private final timer timer = new timer (); private final int min;

Public replytimer (int minutes) {min = minutes; }

public void Start () {Date date = new Date ();  Timer.schedule (New Replytask (), date, min * 60 * 1000); }

public void Stop () {timer.cancel (); }}

----------------------------------------------------------------Replytask.java------------------------------------------- ---------------------

Package com.hanweb.jcms;

Import java.util.*;

public class Replytask extends TimerTask {public void dosomething () {System.out.println ("[SYS] SMS reply Listener ru  Nning "); }

public void Run () {dosomething (); }}

Put the compiled class file into the web-inf/classes and don't forget to add a listener statement to the web.xml of the current Web application in the servlet container:

<listener> <listener-class>com.hanweb.jcms.ReplyListener</listener-class> </listener>




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.