zoukankan      html  css  js  c++  java
  • java web每天定时执行任务(四步轻松搞定)

    java web每天定时执行任务(四步轻松搞定)

     

    第一步:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    package com.eh.util;
     
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Timer;
     
    /**
     * java定时任务,每天定时执行任务
     * @author wls
     *
     */
    public class TimerManager {
        //时间间隔
         private static final long PERIOD_DAY = 24 60 60 1000;
         public TimerManager() {
              Calendar calendar = Calendar.getInstance(); 
                     
              /*** 定制每日2:00执行方法 ***/
     
              calendar.set(Calendar.HOUR_OF_DAY, 16);
              calendar.set(Calendar.MINUTE, 10);
              calendar.set(Calendar.SECOND, 0);
               
              Date date=calendar.getTime(); //第一次执行定时任务的时间
              System.out.println(date);
              System.out.println("before 方法比较:"+date.before(new Date()));
              //如果第一次执行定时任务的时间 小于 当前的时间
              //此时要在 第一次执行定时任务的时间 加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。循环执行的周期则以当前时间为准
              if (date.before(new Date())) {
                  date = this.addDay(date, 1);
                  System.out.println(date);
              }
               
              Timer timer = new Timer();
               
              NFDFlightDataTimerTask task = new NFDFlightDataTimerTask();
              //安排指定的任务在指定的时间开始进行重复的固定延迟执行。
              timer.schedule(task,date,PERIOD_DAY);
             }
     
             // 增加或减少天数
             public Date addDay(Date date, int num) {
              Calendar startDT = Calendar.getInstance();
              startDT.setTime(date);
              startDT.add(Calendar.DAY_OF_MONTH, num);
              return startDT.getTime();
             }
    }

    第二步:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    package com.eh.util;
     
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.TimerTask;
     
    /**
     * 在 TimerManager 这个类里面,大家一定要注意 时间点的问题。如果你设定在凌晨2点执行任务。但你是在2点以后
     *发布的程序或是重启过服务,那这样的情况下,任务会立即执行,而不是等到第二天的凌晨2点执行。为了,避免这种情况
     *发生,只能判断一下,如果发布或重启服务的时间晚于定时执行任务的时间,就在此基础上加一天。
     * @author wls
     *
     */
    public class NFDFlightDataTimerTask extends TimerTask {
        private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        @Override
        public void run() {
            try {
                 //在这里写你要执行的内容
                System.out.println("执行当前时间"+formatter.format(Calendar.getInstance().getTime()));
            catch (Exception e) {
                System.out.println("-------------解析信息发生异常--------------");
            }
        }
         
    }

    第三步:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    package com.eh.util;
     
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
     
     
    public class NFDFlightDataTaskListener implements  ServletContextListener {
     
        public void contextInitialized(ServletContextEvent sce) {
             new TimerManager();
        }
     
        public void contextDestroyed(ServletContextEvent sce) {
            // TODO Auto-generated method stub
             
        }
     
    }

    第四步:配置web.xml文件

    1
    2
    3
    4
    <!--NFDFlightDataTaskListener 监听器-->
    <listener>
        <listener-class>com.eh.util.NFDFlightDataTaskListener</listener-class>
    </listener>
  • 相关阅读:
    批量新增百万条数据 十百万条数据
    sqlserver 组内排序
    EF ++属性会更新实体
    Entity Framework Core: A second operation started on this context before a previous operation completed
    abp Cannot access a disposed object. A common cause of this error is disposing
    abp xunit Can not register IHostingEnvironment. It should be a non-abstract class. If not, it should be registered before.”
    hangfire enqueued but not processing(hangfire 定时任务入队列但不执行)
    EF 更新实体 The instance of entity type 'BabyEvent' cannot be tracked because another instance
    datatable to entiy list 不支持可空类型和枚举类型
    webapi 设置不显示接口到swaggerUI
  • 原文地址:https://www.cnblogs.com/shizhijie/p/7648001.html
Copyright © 2011-2022 走看看