The final function of this class is to execute a function at a certain point in time every day (such as every night.
First, introduceJava timer (Java. util. Timer)Scheduled execution of scheduled tasks is enabled. By setting the timer interval, scheduled tasks are automatically executed after this interval.Pre-scheduled tasks (Java. util. timertask)
For example, run timer. Schedule (timertask, 0, 60*60*1000) every hour ).
The first parameter of the schedule method is the task to be executed. The type of this parameter is Java. util. timertask,
The second parameter is the waiting time before the task is executed. Here 0 indicates no waiting. The third parameter is the interval, in milliseconds.
Because we hope that the timer can start timing automatically when the web project starts, tasks will be executed on a regular basis throughout the life cycle of the web project, therefore, the Timer class cannot be a general class. Here, the servlet listener class is used to start the timer. By configuring the listener in the configuration file, the listener is automatically loaded and run during project startup, the current period is the life cycle of the web project.
To use this listener, you must configure it in Web. XML as follows:
<Listener> <listener-class> com. Demo. Timer. contextlistener </listener-class> </listener>
To use the servlet listener, you need to implement the javax. servlet. servletcontextlistener interface. The following is a class design:
Listener content
Package COM. demo. timer; import javax. servlet. servletcontextevent; import javax. servlet. servletcontextlistener;/*** listener content */public class contextlistener implements servletcontextlistener {Private Static final long serialversionuid = 1l; Public contextlistener () {} private Java. util. timer timer = NULL; Public void contextinitialized (servletcontextevent event) {/*** sets a timer */Timer = new Java. util. timer (true); event. getservletcontext (). log ("timer started");/*** when the timer reaches the specified time, execute an operation (such as a class or method) * /// the last parameter indicates the monitoring cycle of the monitor, which is currently one hour timer. schedule (New mytask (event. getservletcontext (), 0, 60x60*1000); event. getservletcontext (). log ("added task scheduling table");} public void contextdestroyed (servletcontextevent event) {timer. cancel (); system. out. println ("timer destruction"); event. getservletcontext (). log ("timer destruction ");}}
Timer task
Package COM. demo. timer; import Java. util. calendar; import Java. util. timertask; import javax. servlet. servletcontext;/*** inherits the timer task class **/public class mytask extends timertask {public mytask () {super ();} /** indicates that the task is executed at three o'clock */Private Static final int c_schedule_hour = 3; Private Static Boolean isrunning = false; private servletcontext context = NULL; Public mytask (servletcontext context) {This. context = Conte XT;} public void run () {calendar Cal = calendar. getinstance (); If (! Isrunning) {If (c_schedule_hour = Cal. get (calendar. hour_of_day) {isrunning = true; context. log ("start executing a specified task");/*** write the execution task hereCode* // New youcode (). changestate (); isrunning = false; context. log ("End of task execution") ;}} else {context. log ("last task execution has not ended ");}}}