Java timed tasks to perform tasks on a daily schedule

Source: Internet
Author: User

Java timed tasks that perform tasks on a daily schedule. 
public class Timermanager {//time intervalPrivate static final Long Period_day = 24 * 60 * 60 * 1000; Public Timermanager () {Calendar calendar = calendar.getinstance ();/*** customizing Daily 2:00 execution Method ***/Calendar.set (Calendar.hour_of_day, 2);   Calendar.set (calendar.minute, 0);   Calendar.set (Calendar.second, 0); Date Date=calendar.gettime ();//Time of the first execution of a scheduled task //If the time for the first scheduled task is less than the current time//The time of the first scheduled task is added to the day so that the task executes at the next point in time. If one day is not added, the task executes immediately. if (Date.before (new Date ())) {date = This.addday (date, 1);    } Timer timer = new timer (); Nfdflightdatatimertask task = new Nfdflightdatatimertask ();//Schedule The specified task to begin repeating fixed deferred execution at a specified time. Timer.schedule (Task,date,period_day); }//Increase or decrease the number of daysPublic date Addday (date date, int num) {Calendar STARTDT = calendar.getinstance ();   Startdt.settime (date);   Startdt.add (Calendar.day_of_month, num); return Startdt.gettime (); } }

in Timermanager This class, we must pay attention to the time point of the problem. If you set the task to run at 2 o'clock in the morning. But you are releasing the program after 2 o'clock or restarting the service, in which case the task will be executed immediately rather than wait until 2 o'clock in the morning the next day. In order to avoid this, you can only judge if the time to publish or restart the service is later than the time of the scheduled execution of the task, on this basis add one day.
public class Nfdflightdatatimertask extends TimerTask {
private static Logger log = Logger.getlogger (nfdflightdatatimertask.class), @Overridepublic Void Run () { try { c2>//write what you want to do here } catch (Exception e) {
Log.info ("-------------Parsing information Exception--------------");
}}}public class Nfdflightdatatasklistener implements Servletcontextlistener {public void contextinitialized ( Servletcontextevent event) { new Timermanager ()}, public void contextdestroyed (Servletcontextevent event) {}}


Then you configure the listener in Web. xml
<listener> <listener-class>  Com.listener.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.