Java web regularly executes tasks every day (four steps are easy to handle) and four steps are easy to handle

Source: Internet
Author: User

Java web regularly executes tasks every day (four steps are easy to handle) and four steps are easy to handle

Step 1:

Package com. eh. util; import java. util. calendar; import java. util. date; import java. util. timer;/*** java scheduled task, daily scheduled task execution * @ author wls **/public class TimerManager {// time interval private static final long PERIOD_DAY = 24*60*60*1000; public TimerManager () {Calendar calendar = Calendar. getInstance ();/*** customize the daily execution method ***/calendar. set (Calendar. HOUR_OF_DAY, 16); calendar. set (Calendar. MINUTE, 10); calendar. Set (Calendar. SECOND, 0); Date date = calendar. getTime (); // The time at which the scheduled task is executed for the first time, System. out. println (date); System. out. println ("before Method Comparison:" + date. before (new Date (); // if the time for the first scheduled task to be executed is less than the current time, // Add one day to the time for the first scheduled task to be executed, this task can be executed at the next time point. If one day is not added, the task will be executed immediately. The cycle of cyclic execution is based on the current time. if (date. before (new Date () {date = this. addDay (date, 1); System. out. println (date);} Timer timer = new Timer (); NFDFlightDataTimerTask task = new NFDFlightDataTimerTask (); // schedule the specified task to execute the same fixed delay at the specified time. Timer. schedule (task, date, PERIOD_DAY);} // increase or decrease the number of days public Date addDay (Date date, int num) {Calendar startDT = Calendar. getInstance (); startDT. setTime (date); startDT. add (Calendar. DAY_OF_MONTH, num); return startDT. getTime ();}}

Step 2:

Package com. eh. util; import java. text. simpleDateFormat; import java. util. calendar; import java. util. timerTask;/*** in the TimerManager class, you must pay attention to the time point. If you want to execute the task at a.m. However, if you release a program or restart the service after, the task will be executed immediately instead of waiting until the next day. To avoid this situation, you can only determine that if the time for publishing or restarting the service is later than the scheduled time for executing the task, add one day. * @ Author wls **/public class NFDFlightDataTimerTask extends TimerTask {private static SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "); @ Overridepublic void run () {try {// here write the content you want to execute System. out. println ("current execution time" + formatter. format (Calendar. getInstance (). getTime ();} catch (Exception e) {System. out. println ("------------- parsing information exception --------------");}}}

Step 3:

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

Step 4: configure the web. xml file

<! -- NFDFlightDataTaskListener listener --> <listener-class> com. eh. util. NFDFlightDataTaskListener </listener-class> </listener>

  

  

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.