Java Web Timer

Source: Internet
Author: User

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

  1. Import java. Io. ioexception;
  2. Import java. util. timer;
  3. Import javax. servlet. requestdispatcher;
  4. Import javax. servlet. servletcontext;
  5. Import javax. servlet. servletexception;
  6. Import javax. servlet. http. httpservlet;
  7. Import javax. servlet. http. httpservletrequest;
  8. Import javax. servlet. http. httpservletresponse;
  9. Import org. Apache. commons. Lang. stringutils;
  10. Public class convergedataservlet extends httpservlet {
  11. Private Static final long serialversionuid = 1l;
  12. Private timer timer1 = NULL;
  13. Private task task1;
  14. /**
  15. * Constructor of the object.
  16. */
  17. Public convergedataservlet (){
  18. Super ();
  19. }
  20. /**
  21. * Destruction of the servlet.
  22. */
  23. Public void destroy (){
  24. Super. Destroy ();
  25. If (timer1! = NULL ){
  26. Timer1.cancel ();
  27. }
  28. }
  29. Public void doget (httpservletrequest request, httpservletresponse response)
  30. Throws servletexception, ioexception {
  31. }
  32. Public void dopost (httpservletrequest request, httpservletresponse response)
  33. Throws servletexception, ioexception {
  34. Doget (request, response );
  35. }
  36. // Start the timer using the init Method
  37. Public void Init () throws servletexception {
  38. Servletcontext context = getservletcontext ();
  39. // (True is to refresh the cache at a specified time)
  40. String starttask = getinitparameter ("starttask ");
  41. // Timed refresh time (minutes)
  42. Long Delay = long. parselong (getinitparameter ("delay "));
  43. // Start the timer
  44. If (starttask. Equals ("true ")){
  45. Timer1 = new timer (true );
  46. Task1 = new task (context );
  47. Timer1.schedule (task1, delay x 60*1000, delay * 60*1000 );
  48. }
  49. }
  50. }

2. Task

  1. Import java. util. timertask;
  2. Import javax. servlet. servletcontext;
  3. Public class task extends timertask {
  4. Private servletcontext context;
  5. Private Static Boolean isrunning = true;
  6. Public task (servletcontext context ){
  7. This. Context = context;
  8. }
  9. @ Override
  10. Public void run (){
  11. If (isrunning ){
  12. }
  13. }
  14. }

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>

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.