Java performs tasks on a timed basis and performs tasks at regular intervals

Source: Internet
Author: User

Java performs tasks on a timed basis, with the following examples of the tasks scheduled for each day:

1. Establish the Timermanage class, set the time-point, the time-point settings of the management class, the code is as follows:

1  PackageCom.pcitc.time;2 3 ImportJava.util.Calendar;4 Importjava.util.Date;5 ImportJava.util.Timer;6 7  Public classTimermanager {8      9      //time intervalTen      Private Static Final LongPeriod_day = 24 * 60 * 60 * 1000; One        A       PublicTimermanager () { -Calendar Calendar =calendar.getinstance (); -              the       /*** Customized 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 A         at       //if the time to perform the first scheduled task is less than the current time -       //At this point, the time for 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 (NewDate ())) { -Date = This. Addday (Date, 1); -       } -         inTimer timer =NewTimer (); -         toNfdflightdatatimertask task =NewNfdflightdatatimertask (); +       //schedules the specified task to begin repeating fixed deferred execution at a specified time.  - Timer.schedule (task,date,period_day); the      } *       $      //increase or decrease the number of daysPanax Notoginseng       PublicDate Addday (date date,intnum) { -Calendar STARTDT =calendar.getinstance (); the startdt.settime (date); + startdt.add (calendar.day_of_month, num); A       returnstartdt.gettime (); the      } +        -}

In Timermanager this class, be sure to pay attention to the point of time. 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.

2. Create the Nfdflightdatatimertask class, and the class that invokes the business code is placed inside the run method of the class.

1  PackageCom.pcitc.time;2 3 ImportJava.util.TimerTask;4 5 ImportCom.sun.istack.internal.logging.Logger;6 7  Public classNfdflightdatatimertaskextendsTimerTask {8 9     Private StaticLogger log = Logger.getlogger (nfdflightdatatimertask.class);Ten  One @Override A      Public voidrun () { -         Try { -             //write what you want to do here. theSystem.out.println ("Functional method written here ..."); -}Catch(Exception e) { -Log.info ("-------------Parsing information Exception--------------"); -         } +     } -}

3. In a Web container, the context listener can be configured to load the listener at startup, with the following code:

1  PackageCom.pcitc.time;2 3 Importjavax.servlet.ServletContextEvent;4 ImportJavax.servlet.ServletContextListener;5 6  Public classNfdflightdatatasklistenerImplementsServletcontextlistener {7 8      Public voidcontextinitialized (Servletcontextevent event) {9         NewTimermanager ();Ten     } One  A      Public voidcontextdestroyed (Servletcontextevent event) { -     } -  the}

4. Configure the Listener in Web. XML

< Listener > < Listener-class > </listener-class > </listener >     

Java performs tasks on a timed basis and performs tasks at regular intervals

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.