Java Web executes tasks on a daily schedule

Source: Internet
Author: User

The first step:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 packagecom.eh.util;importjava.util.Calendar;importjava.util.Date;importjava.util.Timer;/** * java定时任务,每天定时执行任务 * @author wls * */publicclassTimerManager {    //时间间隔     privatestaticfinallong PERIOD_DAY = 2460601000;     publicTimerManager() {          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(newDate()));          //如果第一次执行定时任务的时间 小于 当前的时间          //此时要在 第一次执行定时任务的时间 加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。循环执行的周期则以当前时间为准          if(date.before(newDate())) {              date = this.addDay(date, 1);              System.out.println(date);          }                    Timer timer = newTimer();                    NFDFlightDataTimerTask task = newNFDFlightDataTimerTask();          //安排指定的任务在指定的时间开始进行重复的固定延迟执行。          timer.schedule(task,date,PERIOD_DAY);         }         // 增加或减少天数         publicDate addDay(Date date, intnum) {          Calendar startDT = Calendar.getInstance();          startDT.setTime(date);          startDT.add(Calendar.DAY_OF_MONTH, num);          returnstartDT.getTime();         }}

Step Two:

1234567891011121314151617181920212223242526 package com.eh.util;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.TimerTask;/** * 在 TimerManager 这个类里面,大家一定要注意 时间点的问题。如果你设定在凌晨2点执行任务。但你是在2点以后 *发布的程序或是重启过服务,那这样的情况下,任务会立即执行,而不是等到第二天的凌晨2点执行。为了,避免这种情况 *发生,只能判断一下,如果发布或重启服务的时间晚于定时执行任务的时间,就在此基础上加一天。 * @author wls * */publicclassNFDFlightDataTimerTask extendsTimerTask {    privatestatic SimpleDateFormat formatter = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");    @Override    publicvoidrun() {        try{             //在这里写你要执行的内容            System.out.println("执行当前时间"+formatter.format(Calendar.getInstance().getTime()));        catch(Exception e) {            System.out.println("-------------解析信息发生异常--------------");        }    }    }

Step Three:

123456789101112131415161718 packagecom.eh.util;importjavax.servlet.ServletContextEvent;importjavax.servlet.ServletContextListener;publicclassNFDFlightDataTaskListener implementsServletContextListener {    publicvoidcontextInitialized(ServletContextEvent sce) {         new TimerManager();    }    publicvoidcontextDestroyed(ServletContextEvent sce) {        // TODO Auto-generated method stub            }}

Fourth Step: Configure the Web. xml file

1234 <!--NFDFlightDataTaskListener 监听器--><listener>    <listener-class>com.eh.util.NFDFlightDataTaskListener</listener-class></listener>

Java Web executes tasks on a daily schedule

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.