A lot of friends have used Windows's mission plan, and there are many mini-trammers who have written the clock alarm. The system automatically shuts down the fun program, but there are very few friends to achieve similar functions in the web engineering. When the web engineering starts, the timer can automatically start timing, in the life period of the entire web project, the timer can trigger a task every night. Therefore, the storage position of the timer is also worth examined, and it is not possible to exist in a single servlet or JavaBean. It must be able to make the regulator host's survival period of the entire web engineering life, which can be automatically loaded automatically when the project is started. Combined with these two points, the listener associated with the servlet context is the most appropriate, by reasonable configuration in the project's configuration file, will automatically run at the engineering startup, and in the monitoring state throughout the project life.
The Servlet listener combines the Java timer to describe the entire implementation process. To use the servlet listener, you need to implement the Javax.Servlet.ServletContextListener interface, and implement two interface functions for its ContextInitialized (ServletContext Event) and ContextDestroyed (ServletContext Event). Considering the process of establishing and destroying the timer, watching the two interface functions, it is not suspected to set the established process into contextIzitialized, and put the destruction process into contedentroyed. I named the implementation class of ServletContextListener as ContextListener, add a timer in it, as follows: import java.util.timer;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet;
Public class contextListener Extends httpservlet imports servletcontextListener {public contextListener () {}
private java.util.Timer timer = null; public void contextInitialized (ServletContextEvent event) {timer = new java.util.Timer (true);. event.getServletContext () log ( "timer has started"); timer.schedule ( New mytask (Event.getServletContext (), 0, 60 * 60 * 1000); Event.getServletContext (). log ("Add Task Scheduling Table");}
Public void contextDestroyed (servletContextevent Event) {timer.cancel (); event.getServletContext (). log ("Timer Destruction");}
} In the above code, time.schedule (new mytask (time.getservletContext (), 0, 60 * 60 * 1000) This behavior timer scheduling statement, where MyTask is a custom task that needs to be scheduled (in mine) In the financial data center project, the report computing engine entrance is inherited, from java.util.timertask, the following will focus, the third parameter is triggered once per hour (ie, 60 * 60 * 1000 milliseconds), the intermediate parameter 0 said no delay. Other code is quite simple, no longer detailed. Here's the implementation of MyTask. In the above code, when constructing MyTask, it is incorporated into the Javax.Servlet.ServletContext type parameter, which is incompatible for the record servlet log, so you need to overload the MYTASK constructor (its parent class) The Java.util.Timertask original constructor is no parameter). In the schedule of Timer.Schedule (), set once per hour, so if you want to implement the scheduled task every 24 hours, it is necessary to judge the clock point, indicated by constant c_schedule_Hour (12 o'clock in the evening, 0 points) ). At the same time, in order to prevent it from being executed 24 hours, the task has not been executed (of course, the general task is not so long), avoiding the second time being scheduled to cause conflicts, setting the status flag ISRunning that is currently executing. Sample code is as follows: import java.util *; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax. .servlet.http.HttpServlet; import javax.servlet *;. public class MyTask extends TimerTask {private static final int C_SCHEDULE_HOUR = 0; private static boolean isRunning = false; private ServletContext context = null;
Public mytask () {} public mytask (servletContext context) {this.context = context;}
Public void Run () {Calendar Cal = Calendar.GetInstance (); if (! isrunning) {if (c_schedule_hour == Cal.get (Calendar.Hour_Of_Day)) {isrunning = true; context.log ("Started to perform the specified task" ); // TODO adds a custom detailed task, the following is only the example // system timing reception mail email email = new email (); email.recieve (); Email.Recieve ();
ISRUNNING = false; context.log ("Specify Task Execution");}} else {context.log ("The last task is not yet ended");}}
} The code from ServletContextListener and MyTask is complete. The last step is to deploy servletContextListener to your web project, add the following three lines in your web.xml configuration file: