An experience I will share with you today is the timing task of Java, which means that the following code is executed at a certain time in a day.
Public class timermanager {// Time interval private static final long period_day = 24 * 60 * 60 * 1000; public timermanager () {Calendar Calendar.GetinStance (). ; /*** Customized daily execution method of 2:00 ** */Calendar.Set (Calendar.hour_of_Day, 2); Calendar.Set (Calendar.minute, 0); Calendar.Set (Calendar.Second, 0); Date date = Calendar.gettime (); // Walk The time of time task // If the time of the first execution of the timing task is less than the current time // At this time, the time of the timing task is added for one day, so that this task can be performed at the next time point. If you do not add a day, the task will be executed immediately. if (date.before (new date ())) {date = this.adddy (date, 1);} timer timer = new timeer (); // Arrange the specified task in specified Time starts to perform repeated fixed delay execution. Timer.scheDule (Task, Date, Period_Day);} // increase or reduce the number of days. (Date); Startdt.add (Calendar .Day_of_month, num); Return Startdt.gettime ();}}
In the TimerManager class, everyone must pay attention to the problem of time. If you set the task at 2 am. But you are published after 2 o'clock or restarted services. In this case, the task will be executed immediately instead of waiting for 2 am the next day. In order to avoid this situation, it can only be judged that if the time of release or restarting the service is later than the time to perform tasks, add one day on this basis.
Public Class NFDFLIGHTATATASK Extends Timrtask {Private Static Logger Log = Logger.GetLogger (nfdflightatatimrtask.classs); c void run () {try {// Write the content you want to execute here} Catch (Exception E) {log.info ("--------------- Analysis of the abnormalities of the analysis information ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"); Ized (ServiceContextevent Event) {new timermanager ();} Public void ContextDestroyed (servicetcontexTevent Event) {}}}
Then configure a monitor in web.xml
<listener> <Listener-Class> Com.listener.nfdflightdatatasklistener </Listener-Class> </Listener>
The above is all the contents of this article. I hope it will be helpful to everyone's learning.