Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to perform tasks on a regular basis by java web

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how java web can perform tasks regularly every day. I hope you will get something after reading this article. Let's discuss it together.

Step one:

Package com.eh.util;import java.util.Calendar;import java.util.Date;import java.util.Timer;/** * java scheduled tasks, daily scheduled tasks * @ author wls * * / public class TimerManager {/ / interval private static final long PERIOD_DAY = 24 * 60 * 60 * 1000; public TimerManager () {Calendar calendar = Calendar.getInstance () / * customize daily 2:00 execution method * * / 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 scheduled task execution System.out.println (date); System.out.println ("before method comparison:" + date.before (new Date () / / if the time to execute the scheduled task for the first time is less than the current time / / add one day to the time when the scheduled task is executed for the first time, so that the task can be executed at the next point in time. If you don't add one day, the task will be carried out 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 be executed with repeated fixed delays 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 category, you must pay attention to the timing. If you're set to perform the mission at 2: 00 in the morning. But if you released the program or restarted the service after 2: 00 a. M., the task will be executed immediately instead of waiting until 2: 00 am the next day. In order to avoid this situation, it can only be judged that if the release or restart of the service is later than the scheduled execution time 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 what you want to execute here System.out.println ("execution current time" + formatter.format (Calendar.getInstance (). GetTime () } catch (Exception e) {System.out.println ("- an exception occurred in parsing information -");}

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

Com.eh.util.NFDFlightDataTaskListener has finished reading this article, I believe you have a certain understanding of "java web how to perform tasks regularly every day". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report