It is common to execute tasks by using a timer. The following is a simple example:
The listener, timertask, and timer classes are used for implementation. In addition, Spring provides advanced timer implementation, which seems to be quarts.
Package Com. Rx. timer;
ImportJava. util. date;
ImportJava. util. timertask;
Public Class Mytask Extends Timertask {
@ Override
Public Void Run (){
System. Out. println ( " Call " + ( New Date ()));
}
}
PackageCom. Rx. timer;
ImportJava. util. timer;
ImportJavax. servlet. servletcontextevent;
ImportJavax. servlet. servletcontextlistener;
Public ClassMylistenImplementsServletcontextlistener {
PrivateTimer= Null;
@ Override
Public VoidContextdestroyed (servletcontextevent arg0 ){
Timer. Cancel ();
}
@ Override
Public Void Contextinitialized (servletcontextevent arg0 ){
Timer = New Timer ( True );
// Set the task schedule, start time, And Interval
Timer. Schedule ( New Mytask (), 0 , 1000 * 10 );
}
}
<? XML version = "1.0" encoding = "UTF-8" ?>
< Web-app ID = "Webapp_id" Version = "2.4" Xmlns = "Http://java.sun.com/xml/ns/j2ee" Xmlns: xsi = "Http://www.w3.org/2001/XMLSchema-instance" Xsi: schemalocation = "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< Display-name >
Webtimer </ Display-name >
< Listener >
< Listener-class > Com. Rx. Timer. mylisten </ Listener-class >
</ Listener >
welcome-file-list >
welcome-file > index. JSP welcome-file >
welcome-file-list >
Web-app >
Well, another complicated one! It is basically the same as above, but it is more detailed in task execution control.
Public Class Workserviceimpl Implements Workservice, servletcontextlistener {
Public VoidContextdestroyed (servletcontextevent arg0 ){
Timer. Cancel ();
System. Out. println ("timer destroyed ");
}
Public void contextinitialized (servletcontextevent event) {
timer = New JAVA. util. timer ( true );
sampletask = New sampletask (event. getservletcontext ();
system. out. println ("timer started");
timer. schedule (sampletask, 0, 60x60*1000);
system. out. println ("task scheduling table added");
}< BR >}
ClassSampletaskExtendsTimertask {
Private Servletcontext context;
Private Static Boolean Isrunning = False ; // Whether it is running
Private Static Boolean Flag =True ; // Never run once
Private Static Final Int C_schedule_hour = 23; // running time
Private Workserviceimpl workservice;
PublicSampletask (servletcontext context ){
This. Context = context;
}
Public Void Run (){
Workservice = New Workserviceimpl ();
Calendar Cal = calendar. getinstance ();
If (! Isrunning ){
If (C_schedule_hour = Cal. Get (calendar. hour_of_day) & flag ){
Isrunning = True ;
Workservice. autoworkoff ();
Isrunning = False ;
Flag = False ;
Context. Log ("End of task execution ");
}
} Else {
Context. Log ("the last task has not been completed ");
}
If (C_schedule_hour! = Cal. Get (calendar. hour_of_day )){
Flag = True ;
}
}
}
Configure in Web. XML as follows:
<Listener>
<Listener-class> com.css. WAM. Service. impl. workserviceimpl </listener-class>
</Listener>