- 论坛徽章:
- 0
|
Java 定时器 - 1.package com.xuanzhi.userstat.spweekly;
- 2.
- 3.import java.util.Timer;
- 4.
- 5.import javax.servlet.ServletContextEvent;
- 6.import javax.servlet.ServletContextListener;
- 7.
- 8.public class TimerListener implements ServletContextListener {
- 9.
- 10. private Timer timer = null;
- 11. private SampleTask sampletask;
- 12. public static String configPath = "";
- 13. @Override
- 14. public void contextDestroyed(ServletContextEvent event) {
- 15. // TODO Auto-generated method stub
- 16. timer.cancel();
- 17. event.getServletContext().log("定时器销毁");
- 18. }
- 19.
- 20. @Override
- 21. public void contextInitialized(ServletContextEvent event) {
- 22. // TODO Auto-generated method stub
- 23.
- 24. configPath = event.getServletContext().getRealPath("/")+"upload/splist.xls";
- 25. System.out.println(configPath);
- 26.
- 27. timer = new Timer(true);
- 28.
- 29. sampletask = new SampleTask(event.getServletContext());
- 30. event.getServletContext().log("定时器已启动");
- 31.
- 32. timer.schedule(sampletask, 0,60*60);
- 33.
- 34. event.getServletContext().log("已添加任务调度表");
- 35.
- 36. }
- 37.
- 38.}
复制代码 package com.xuanzhi.userstat.spweekly;- import java.util.Timer;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- public class TimerListener implements ServletContextListener {
- private Timer timer = null;
- private SampleTask sampletask;
- public static String configPath = "";
- @Override
- public void contextDestroyed(ServletContextEvent event) {
- // TODO Auto-generated method stub
- timer.cancel();
- event.getServletContext().log("定时器销毁");
- }
- @Override
- public void contextInitialized(ServletContextEvent event) {
- // TODO Auto-generated method stub
-
- configPath = event.getServletContext().getRealPath("/")+"upload/splist.xls";
- System.out.println(configPath);
-
- timer = new Timer(true);
-
- sampletask = new SampleTask(event.getServletContext());
- event.getServletContext().log("定时器已启动");
-
- timer.schedule(sampletask, 0,60*60);
-
- event.getServletContext().log("已添加任务调度表");
-
- }
- }
复制代码 Java代码- 1.package com.xuanzhi.userstat.spweekly;
- 2.
- 3.import java.text.ParseException;
- 4.import java.text.SimpleDateFormat;
- 5.import java.util.Calendar;
- 6.import java.util.Date;
- 7.import java.util.TimerTask;
- 8.
- 9.import javax.servlet.ServletContext;
- 10.
- 11.import com.xuanzhi.userstat.dailydata.MailUtil;
- 12.
- 13.public class SampleTask extends TimerTask {
- 14.
- 15. public SampleTask(ServletContext context) {
- 16. this.context = context;
- 17. }
- 18.
- 19. private ServletContext context;
- 20. private static boolean isRunning = false;
- 21. private static boolean flag = true;
- 22. private static final int WEEK_DAY = 2;
- 23.
- 24. @Override
- 25. public void run() {
- 26. // TODO Auto-generated method stub
- 27. SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd");
- 28. Calendar cal = Calendar.getInstance();
- 29. cal.setTime(new Date());
- 30.
- 31.
- 32. if(!isRunning)
- 33. {
- 34. if(WEEK_DAY == cal.get(Calendar.DAY_OF_WEEK) && flag)
- 35. {
- 36. isRunning = true;
- 37. context.log("开始执行指定任务");
- 38.
- 39. //需要执行的代码
- 40. getSpContent gs = new getSpContent();
- 41. String mailCountent = gs.sendMail(TimerListener.configPath);
- 42.
- 43. MailUtil.sendMail("mengfanzhi@yolotone.com","","", "通道收入周报 "+fm.format(new Date()),mailCountent , null);
- 44.
- 45. isRunning = false;
- 46. flag = false;
- 47. context.log("指定任务执行结束");
- 48. }
- 49. }
- 50. else
- 51. {
- 52. context.log("上一次任务执行还未结束");
- 53. }
- 54.
- 55. if(WEEK_DAY != cal.get(Calendar.DAY_OF_WEEK))
- 56. {
- 57. flag = true;
- 58. }
- 59. }
- 60.
- 61.}
- package com.xuanzhi.userstat.spweekly;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.TimerTask;
- import javax.servlet.ServletContext;
- import com.xuanzhi.userstat.dailydata.MailUtil;
- public class SampleTask extends TimerTask {
- public SampleTask(ServletContext context) {
- this.context = context;
- }
- private ServletContext context;
- private static boolean isRunning = false;
- private static boolean flag = true;
- private static final int WEEK_DAY = 2;
-
- @Override
- public void run() {
- // TODO Auto-generated method stub
- SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd");
- Calendar cal = Calendar.getInstance();
- cal.setTime(new Date());
-
- if(!isRunning)
- {
- if(WEEK_DAY == cal.get(Calendar.DAY_OF_WEEK) && flag)
- {
- isRunning = true;
- context.log("开始执行指定任务");
-
- //需要执行的代码
- getSpContent gs = new getSpContent();
- String mailCountent = gs.sendMail(TimerListener.configPath);
-
- MailUtil.sendMail("mengfanzhi@yolotone.com","","", "通道收入周报 "+fm.format(new Date()),mailCountent , null);
-
- isRunning = false;
- flag = false;
- context.log("指定任务执行结束");
- }
- }
- else
- {
- context.log("上一次任务执行还未结束");
- }
-
- if(WEEK_DAY != cal.get(Calendar.DAY_OF_WEEK))
- {
- flag = true;
- }
- }
- }
复制代码 最后在web.xml中配置监听器
Java代码- 1.<listener>
- 2.<listener-class>com.xuanzhi.userstat.spweekly.TimerListener</listener-class>
- 3.</listener>
复制代码 |
|