当前位置: 代码迷 >> Web前端 >> 怎么在Web程序中实现定时运行的功能
  详细解决方案

怎么在Web程序中实现定时运行的功能

热度:605   发布时间:2012-11-12 12:31:58.0
如何在Web程序中实现定时运行的功能?

定时器在Web中的应用

关键字: java定时器在web中的应用
在web中,定时器的启动一般随web server的启动而启动,一般有两种方法.?

方法一:在web.xml里配置一个Servlet,并设置其随web server的启动而启动。然后在该Servlet的init()方法里启动定时器,在destory()方法里销毁定时器。?
方法二:在web.xml里配置一个Listener,然后在该Listener的初始化方法里启动定时器,在其销毁的方法朝左销毁定时器。?

在servlet中启动定时器?
Xml代码?
  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. ?????
  10. import?org.apache.commons.lang.StringUtils;??????
  11. ?????
  12. public?class?ConvergeDataServlet?extends?HttpServlet?{??????
  13. ?????
  14. ????private?static?final?long?serialVersionUID?=?1L;??????
  15. ?????
  16. ????private?Timer?timer1?=?null;??????
  17. ??????????
  18. ????private?Task?task1;??????
  19. ??????????
  20. ????/**?????
  21. ?????*?Constructor?of?the?object.?????
  22. ?????*/?????
  23. ????public?ConvergeDataServlet()?{??????
  24. ????????super();??????
  25. ????}??????
  26. ?????
  27. ????/**?????
  28. ?????*?Destruction?of?the?servlet.???
  29. ????
  30. ?????*/?????
  31. ????public?void?destroy()?{??????
  32. ????????super.destroy();???????
  33. ????????if(timer1!=null){??????
  34. ????????????timer1.cancel();??????
  35. ????????}??????
  36. ????}??????
  37. ?????
  38. ??????????
  39. ????public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)??????
  40. ????????????throws?ServletException,?IOException?{??????
  41. ??????????????
  42. ????}??????
  43. ?????
  44. ??????????
  45. ????public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)??????
  46. ????????????throws?ServletException,?IOException?{??????
  47. ????????doGet(request,?response);?????????????
  48. ????}??????
  49. ?????
  50. ????//?init方法启动定时器??????
  51. ????public?void?init()?throws?ServletException?{??????
  52. ??????????????
  53. ????????ServletContext?context?=?getServletContext();??????
  54. ??????????????
  55. ????????//?(true为用定时间刷新缓存)??????
  56. ????????String?startTask?=?getInitParameter("startTask");??????
  57. ??????????????????????
  58. ????????//?定时刷新时间(分钟)??????
  59. ????????Long?delay?=?Long.parseLong(getInitParameter("delay"));??????
  60. ??????????????
  61. ????????//?启动定时器??????
  62. ????????if(startTask.equals("true")){??????
  63. ????????????timer1?=?new?Timer(true);??????
  64. ????????????task1?=?new?Task(context);??????
  65. ????????????timer1.schedule(task1,?delay?*?60?*?1000,?delay?*?60?*?1000);??????
  66. ????????}??????
  67. ????}??????
  68. }??????

定时执行操作?
?????
Java代码?
  1. /**????
  2. ?*?????
  3. ?*?@author?callan????
  4. ?*/?????
  5. import?java.util.TimerTask;??????
  6. ?????
  7. import?javax.servlet.ServletContext;??????
  8. ?????
  9. public?class?Task?extends?TimerTask{??????
  10. ?????
  11. ????private?ServletContext?context;??????
  12. ??????????
  13. ????private?static?boolean?isRunning?=?true;??????
  14. ??????????
  15. ????public?Task(ServletContext?context){??????
  16. ????????this.context?=?context;??????
  17. ????}??????
  18. ??????????
  19. ??????????????
  20. ????@Override?????
  21. ????public?void?run()?{??????
  22. ????????if(isRunning){??????
  23. ????????????????
  24. ????????}??????
  25. ????}??????
  26. ?????
  27. }??????

在web.xml配置这个servlet为服务启动时就调用servlet?

< 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 >?

  相关解决方案