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>