Tomcat scheduled task-servletcontextlistener

Source: Internet
Author: User

Syscontextlistener. Java

 

 Package com. tq365.util;

Import java. util. timer;

Import javax. servlet. servletcontextevent;
Import javax. servlet. servletcontextlistener;

Import com. tq365.test. task;

Public class syscontextlistener implements servletcontextlistener {
Private timer = NULL;

// Initialize the listener here. The Listener starts when Tomcat is started. The timer function can be implemented here.
Public void contextinitialized (servletcontextevent event ){
System. Out. println ("listener has been initialized! ");
Timer = new timer ();
Event. getservletcontext (). Log ("Tomcat timer started .....");
// Call exporthistorybean. 0 indicates that the task has no delay. 5*1000 indicates that the task is executed every 5 seconds. 60*60*1000 indicates that the task is executed every 5 seconds.
// Timer. Schedule (New specifiedtask (), 0, 5*1000 );
// Timer. Schedule (new task (), 0, 10*1000 );
Event. getservletcontext (). Log ("the task has been added .....");
}
Public void contextdestroyed (servletcontextevent event ){
// Disable the listener and the timer.
Timer. Cancel ();
Event. getservletcontext (). Log ("timer off .....");
}
}

Specifiedtask. Java scheduled task

 

 Package com. tq365.util;
Import java. util. timertask;
Import com. tq365.mail. Sendmail;

Public class specifiedtask extends timertask {
Private Static Boolean isrunning = false;
Public specifiedtask (servletcontext context ){
This. Context = context;
}*/
@ Override
Public void run (){
If (! Isrunning ){
Isrunning = true;
System. Out. println ("timed print ...........");
Sendmail. Sendmail (); // send emails regularly
}
}

}

 

 

 

Related Article

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.