This article introduces the Java web to execute tasks regularly every day and share them with you. The details are as follows:
first step:
package com.eh.util;import java.util.Calendar;import java.util.Date;import java.util.Timer;/** * java timed tasks, execute tasks regularly every day* @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 2:00 ***/ calendar.set(Calendar.HOUR_OF_DAY, 16); calendar.set(Calendar.MINUTE, 10); calendar.set(Calendar.SECOND, 0); Date date=calendar.getTime(); //Time of the first time of execution of the timing task System.out.println(date); System.out.println("before method comparison: "+date.before(new Date())); //If the time of execution of the timing task for the first time is less than the current time//At this time add one day to the first time of execution of the timing task so that this task can be executed at the next time point. If there is no additional day, the task will be executed immediately. The cycle of loop 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 start repeated fixed delay execution 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, everyone must pay attention to the issue of time. If you set to perform tasks at 2 a.m. But if you released the program after 2 o'clock, or restarted the service, the task will be executed immediately, rather than waiting until 2 o'clock the next day. In order to avoid this situation, we can only judge that if the time for release or restart of the service is later than the time for the time of execution of the task, add one day on this basis. * @author wls * */public class NFDFlightDataTimerTask extends TimerTask { private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public void run() { try { // Write the content you want to execute here System.out.println("Execution current time"+format(Calendar.getInstance().getTime())); } catch (Exception e) { System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Step 3:
package com.eh.util;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class NFDFlightDataTaskListener implements ServletContextListener { public void contextInitialized(ServletContextEvent scce) { new TimerManager(); } public void contextDestroyed(ServletContextEvent scce) { // TODO Auto-generated method stub } }Step 4: Configure the web.xml file
<!--NFDFlightDataTaskListener listener--><listener> <listener-class>com.eh.util.NFDFlightDataTaskListener</listener-class></listener>
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.