Use servlet context to implement listeners

Source: Internet
Author: User

In addition to spring scheduling, you can also use servlet context to implement scheduled tasks in projects.

1. Create a listener class to implement the servletcontextlistener interface and implement the methods in the interface:

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

/**
* The Listener class and timer are combined to regularly execute tasks.
*/
Public class contextlistener implements servletcontextlistener {
Private java. util. Timer timer = NULL;
Public void contextinitialized (servletcontextevent arg0 ){
Timer = new java. util. Timer (true); // create a timer and specify its related threads as daemon programs.
// System. Out. println ("START timer ");
// Scheduler, earlywarningtask () is a custom scheduling task
// Execute a task during initialization
Timer. Schedule (New earlywarningtask (), 10, 60*60*1000); // the delay before the task is executed is 10 milliseconds, and the interval between tasks is 60*60*1000 milliseconds.
// System. Out. println ("scheduling task added successfully ");
}
Public void contextdestroyed (servletcontextevent arg0 ){
Timer. Cancel ();
// System. Out. println ("scheduled task destruction ");

}

}

2. Obtain the applicationcontext configuration information of spring to read the related business methods in the configuration file in the scheduling task.

Import org. springframework. Context. Support. abstractapplicationcontext;
Import org. springframework. Context. Support. classpathxmlapplicationcontext;

Public class appcontext {
Private Static appcontext instance;

Private abstractapplicationcontext appcontext;

Public synchronized static appcontext getinstance (){
If (instance = NULL ){
Instance = new appcontext ();
}
Return instance;
}

Private appcontext (){
This. appcontext = new classpathxmlapplicationcontext (
"/Applicationcontext. xml ");
}

Public abstractapplicationcontext getappcontext (){
Return appcontext;
}
}

3. Custom scheduling task class, inheriting timertask class

Import java. util. timertask;

Public class earlywarningtask extends timertask {
Private Static final int c_schedule_hour = 23; // the execution time is 23 hours.
Private Static Boolean isrunning = false;

// Obtain the related business methods in the configuration file through the appcontext class

// Instoragedetailimp
Protected iinstoragedetailservice getinstoragedetailservice (){
Return (iinstoragedetailservice) appcontext. getinstance (). getappcontext ()
. Getbean ("instoragedetailimp ");
}
// Goodswarningimp
Protected igoodswarningservice getgoodswarningservice (){
Return (igoodswarningservice) appcontext. getinstance (). getappcontext ()
. Getbean ("goodswarningimp ");
}
// Lendoutaccounibd
Protected ilendoutaccountservice getlendoutaccountservice (){
Return (ilendoutaccountservice) appcontext. getinstance (). getappcontext ()
. Getbean ("lendoutaccounibd ");
}
// Lendoutwarningimp
Protected ilendoutwarningservice getlendoutwarningservice (){
Return (ilendoutwarningservice) appcontext. getinstance (). getappcontext ()
. Getbean ("lendoutwarningimp ");
}

// The task is implemented in the run () method.
@ Override
Public void run (){
Calendar Cal = calendar. getinstance ();
If (! Isrunning ){
If (c_schedule_hour = Cal. Get (calendar. hour_of_day) {// hour_of_day is used for the 24-hour clock.
Isrunning = true;
System. Out. println ("scheduled task execution ");
// Task code
//..............................
Isrunning = false;
System. Out. println ("scheduled task ended ");
} Else {
System. Out. println ("scheduled task time not reached ");
}
} Else {
System. Out. println ("the previous scheduled task has not ended ");
}
}

4. Web. xml configuration listening

<Listener>
<Listener-class> com. Cl. GDB. util. contextlistener </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.