Project requirements Daily 4 o'clock in the morning reconciliation business, the following are scheduled tasks:
First create the listener class for this class Checkaccountlistener
1 Public classCheckaccountlistenerImplementsServletcontextlistener {2 3 Private StaticCheckaccounttimer timer;4 5 @Override6 Public voidcontextdestroyed (servletcontextevent arg0) {7 Try{8 if(timer!=NULL){9 timer.stop ();TenTimer =NULL; One } A -}Catch(Exception e) { - e.printstacktrace (); the } - - } - + @Override - Public voidcontextinitialized (servletcontextevent arg0) { + Try { ATimer =NewCheckaccounttimer (); at Timer.start (); -}Catch(Exception e) { - e.printstacktrace (); - } - - } in -}
Then create the time method for the class
1 Public classCheckaccounttimerextendsTimer {2 Private BooleanIsrun =false;3 4 Public synchronized Booleanstart () {5 6 Try {7 //specified daily time of 4 o'clock in the morning run8 FinalSimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd ' 04:00:00 '");9 //First Run timeTenDate StartTime =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Parse (Sdf.format (NewDate ())); One //If today's already past the first run time then change to tomorrow A if(System.currenttimemillis () >Starttime.gettime ()) { -StartTime =NewDate (Starttime.gettime () + 1000 * 60 * 60 * 24); - } the - - if(isrun) { -SYSTEM.OUT.PRINTLN ("********** Reconciliation program has been started"); + return true; - } + This. Schedule (NewCheckAccount (), startTime, 1000 * 60 * 60 * 24); AIsrun =true; atSYSTEM.OUT.PRINTLN ("********** Reconciliation program started successfully"); - return true; -}Catch(Exception e) { -SYSTEM.OUT.PRINTLN ("Error in ********** reconciliation program"); - e.printstacktrace (); -Isrun =false; in return false; - } to } + - Public BooleanStop () { the Try { * if(!Isrun) { $SYSTEM.OUT.PRINTLN ("********** Reconciliation program not Started");Panax Notoginseng return true; - } the This. Cancel (); +Isrun =false; ASystem.out.println ("********** stops the reconciliation process successfully"); the return true; +}Catch(Exception e) { -System.out.println ("********** Stop Reconciliation Program error"); $ e.printstacktrace (); $ return false; - } - } the}
Timer Schedule Perform timed tasks
Method that executes only once
Timer.schedule (timertask task, long delay) method is executed only once in the program (long delay is the time difference from the start task)
Note: Schedules the specified task for execution after the specified delay. The main idea is to execute the task after delaying delay of milliseconds.
The Timer.schedule (timertask task, Date time) method is executed only once in the program (date is set to start at)
Repeated execution methods
Timer.schedule (timertask task, long delay, long period) is a repeating execution
Note: Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. The main idea is to repeat the execution of a task after the delay time delay of milliseconds, the period is period milliseconds
Timer.schedule (timertask task, date time, long period) is a repeating execution (date is the start-up)
Configure the Web. xml file
Add a listener under servlet-mapping
1 < Listener > 2 < Listener-class >com.sales.task.CheckAccountListener</listener-class>3 </listener>
The role of monitoring under Web. xml
Configuration one under Web. xml:
1. When you start a Web project, the container (for example, Tomcat) reads its configuration file, Web. Read two nodes: and
2. Immediately after the container creates a servletcontext (context), all parts of the Web project will share this context.
3. The container will be converted to a key-value pair and handed to ServletContext.
4. A class instance in the container creation, that is, the listener is created.
5. The contextinitialized (Servletcontextevent args) initialization method is used in the listener to obtain
ServletContext = Servletcontextevent.getservletcontext ();
The value of Context-param = Servletcontext.getinitparameter ("Key of Context-param");
6. After you get the value of this context-param, you can do something about it. Note that this time your Web project is not fully started yet. This action will be earlier than all servlets.
In other words, this time, your action on the key value will be executed before your Web project is fully booted.
7. You may want to open the database before the project starts.
Then you can set the connection mode of the database in, and initialize the connection of the database in the Listener class.
8. This listener is a class of its own, in addition to the initialization method, it also has the method of destruction. Frees the resource before closing the app. For example, the database connection is closed.
Java Set timed Tasks