java timer 定时任务

  • 监听类
 1 package com.xx.model;
 2 
 3 import java.util.Calendar;
 4 import java.util.Date;
 5 import java.util.Timer;
 6 import javax.servlet.ServletContextEvent;
 7 import javax.servlet.ServletContextListener;
 8 import org.apache.commons.logging.Log;
 9 import org.apache.commons.logging.LogFactory;
10 import com.xx.model.XXTask;
11 
12 public class XXTimerListener  implements ServletContextListener  {
13     
14     private Timer timer = null;
15     private static final Log log = LogFactory.getLog(XXTimerListener.class);
16     final Log logrecords = LogFactory.getLog("records");
17 
18     public void contextDestroyed(ServletContextEvent arg0) {
19         // TODO Auto-generated method stub
20         
21     }
22     
23     public void contextInitialized(ServletContextEvent arg0) {
24         // TODO Auto-generated method stub
25         
26         try {
27             timer = new Timer(true);
28             try {
29                 Calendar calendar = Calendar.getInstance();
30                 calendar.set(Calendar.HOUR_OF_DAY, 10); // 上午10点
31                 calendar.set(Calendar.MINUTE, 0);
32                 calendar.set(Calendar.SECOND, 0);
33                 Date date = calendar.getTime(); // 第一次执行定时任务的时间
34                                                 // 如果第一次执行定时任务的时间 小于当前的时间
35                                                 // 此时要在 第一次执行定时任务的时间加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。
36                 if (date.before(new Date())) {
37                     date = addDay(date, 1);
38                 }
39                 timer.schedule(new XXTask(),date,24*3600*1000);
40                 log.info("任务创建成功!");
41             } catch (Exception e) {
42                 log.error("任务创建失败!", e);
43             }
44         } catch (Exception e) {
45             // TODO Auto-generated catch block
46             e.printStackTrace();
47         }
48     }
49     
50     public Date addDay(Date date, int num) {
51         Calendar startDT = Calendar.getInstance();
52         startDT.setTime(date);
53         startDT.add(Calendar.DAY_OF_MONTH, num);
54         return startDT.getTime();
55     }
56 }
  • 任务类
 1 package com.xx.model;
 2 import java.util.TimerTask;
 3 import org.apache.commons.logging.Log;
 4 import org.apache.commons.logging.LogFactory;
 5 /**
 6  * @author qjl
 7  * 
 8  */
 9 public class XXTask extends TimerTask {
10 
11     private static final Log log = LogFactory.getLog(XXTask.class);
12     MeasureCountsAjax mscAjax = new MeasureCountsAjax();
13 
14     @Override
15     public void run() {
16 
17         log.debug("定时任务" + new Date(System.currentTimeMillis()));
18         try {
19             //定时任务要执行的方法写这里
20         } catch (Exception e) {
21             log.error("定时运行失败", e);
22         } finally {
23         }
24     }
25 }

 

  • 在web.xml中增加
1   <listener>
2          <listener-class>
3               com.xx.model.XXTimerListener
4          </listener-class>
5     </listener>

 

posted @ 2015-07-03 15:53  java小秦  阅读(208)  评论(0编辑  收藏  举报