Java Web timer, the start of the timer is generally started with the start of the web server, there are two methods.
Method 1: configure a servlet in Web. XML, inherit httpservlet, and set it to start with Web server startup. Start the timer in the init () method of the servlet and destroy the timer in the destory () method.
Method 2: configure a listener in Web. XML, inherit from servletcontextlistener, start the timer in the listener initialization method, and destroy the timer from the left to the left.
Start the timer in Servlet
I. inherit the syntax of httpservlet:
1, Servlet
- Import java. Io. ioexception;
- Import java. util. timer;
- Import javax. servlet. requestdispatcher;
- Import javax. servlet. servletcontext;
- Import javax. servlet. servletexception;
- Import javax. servlet. http. httpservlet;
- Import javax. servlet. http. httpservletrequest;
- Import javax. servlet. http. httpservletresponse;
- Import org. Apache. commons. Lang. stringutils;
- Public class convergedataservlet extends httpservlet {
- Private Static final long serialversionuid = 1l;
- Private timer timer1 = NULL;
- Private task task1;
- /**
- * Constructor of the object.
- */
- Public convergedataservlet (){
- Super ();
- }
- /**
- * Destruction of the servlet.
- */
- Public void destroy (){
- Super. Destroy ();
- If (timer1! = NULL ){
- Timer1.cancel ();
- }
- }
- Public void doget (httpservletrequest request, httpservletresponse response)
- Throws servletexception, ioexception {
- }
- Public void dopost (httpservletrequest request, httpservletresponse response)
- Throws servletexception, ioexception {
- Doget (request, response );
- }
- // Start the timer using the init Method
- Public void Init () throws servletexception {
- Servletcontext context = getservletcontext ();
- // (True is to refresh the cache at a specified time)
- String starttask = getinitparameter ("starttask ");
- // Timed refresh time (minutes)
- Long Delay = long. parselong (getinitparameter ("delay "));
- // Start the timer
- If (starttask. Equals ("true ")){
- Timer1 = new timer (true );
- Task1 = new task (context );
- Timer1.schedule (task1, delay x 60*1000, delay * 60*1000 );
- }
- }
- }
2. Task
- Import java. util. timertask;
- Import javax. servlet. servletcontext;
- Public class task extends timertask {
- Private servletcontext context;
- Private Static Boolean isrunning = true;
- Public task (servletcontext context ){
- This. Context = context;
- }
- @ Override
- Public void run (){
- If (isrunning ){
- }
- }
- }
3. configuration in Web. xml
<Servlet>
<Servlet-Name> taskservlet </servlet-Name>
<Servlet-class> com. Task </servlet-class>
<Init-param>
<Param-Name> starttask </param-Name>
<Param-value> true </param-value>
</Init-param>
<Init-param>
<Param-Name> intervaltime </param-Name>
<Param-value> 1 </param-value>
</Init-param>
<Load-on-startup> 300 </load-on-startup>
</Servlet>
Ii. inherit from servletcontextlistener
1. Timer
/**
*
*/
Package com. annlee. Website. util;
Import java. util. timer;
Import javax. servlet. servletcontextevent;
Import javax. servlet. servletcontextlistener;
/**
* @ Author administrator Timer
*/
Public class timerlistener implements servletcontextlistener {
Private timer;
Private annleetimertask task;
Public void contextdestroyed (servletcontextevent arg0 ){
Timer. Cancel ();
System. Out. println ("timer destroyed ");
}
Public void contextinitialized (servletcontextevent event ){
Timer = new java. util. Timer (true );
Task = new annleetimertask (event. getservletcontext ());
System. Out. println ("timer started ");
Timer. Schedule (task, 0, 60*1000 );
System. Out. println ("added task scheduling table ");
}
}
2. Task
Package com. annlee. Website. util;
Import java. util. calendar;
Import java. util. timertask;
Import javax. servlet. servletcontext;
Import com. annlee. Website. model. Service. member. usmaccessdataservice;
/**
* Timer tasks
*/
Public class annleetimertask extends timertask {
Private servletcontext context;
Private Static Boolean isrunning = false;
Private Static Boolean flag = true;
Private Static final int c_schedule_hour = 0;
Private usmaccessdataservice accessdataservice;
Public annleetimertask (servletcontext context ){
This. Context = context;
}
Public void run (){
Accessdataservice = new usmaccessdataservice ();
Calendar Cal = calendar. getinstance ();
If (! Isrunning ){
If (c_schedule_hour = Cal. Get (calendar. hour_of_day) & flag ){
Isrunning = true;
// Accessdataservice. sendhttpbymonth (0, 0 );
System. Out. println ("executing the task ");
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;
}
}
}
3. configuration in Web. xml
<Listener>
<Listener-class>
Com. annlee. Website. util. timerlistener
</Listener-class>
</Listener>