To perform a task interval setting:
Special characters allowed by field allowed values
Seconds 0-59,-*/
Cent 0-59,-*/
Hour 0-23,-*/
Date 1-31,-*? /LW C
Month 1-12 or JAN-DEC,-*/
Week 1-7 or Sun-sat,-*? /L C #
Year (optional) leave blank, 1970-2099,-*/
Meaning of expression
"0 0 12 * *" Every day 12 o'clock noon.
"0 15 10?" * * "Every 10:15 trigger
"0 15 10 * *" Every day 10:15 trigger
"0 15 10 * * * *" every day 10:15 trigger
"0 15 10 * * *" 2005 "2005 Years ' daily 10:15 trigger
"0 * 14 * *" is triggered every 1 minutes from 2 o'clock in the afternoon to 2:59 every day
"0 0/5 14 * *" is triggered every 5 minutes from 2 o'clock in the afternoon to 2:55 every day
"0 0/5 14,18 * *" triggered every 5 minutes from 2 o'clock in the afternoon to 2:55 and 6 o'clock in the afternoon to 6:55 every day
"0 0-5 14 * *" is triggered every 1 minutes from 2 o'clock in the afternoon to 2:05 every day
"0 10,44 14?" 3WED "Wednesday 2:10 and 2:44 triggers per year of March
"0 15 10?" *mon-fri "Monday to Friday 10:15 trigger
"0 15 10 15 *" Every month 15th 10:15 trigger
"0 L *?" triggered 10:15 the last day of every month
"0 15 10?" *6l "The last Friday 10:15 trigger of the month
"0 15 10?" * 6l2002-2005 "2002 to 2005 the last of the monthly Friday 10:15 trigger
"0 15 10?" *6#3 "The third Friday 10:15 trigger per month
The final function of this class is to perform a function at a point in time every day, such as every 22 o'clock in the evening.
First, the Java Timer (java.util.Timer) has the ability to perform scheduled tasks regularly, and by setting the timer interval, the scheduled tasks are automatically performed after this interval (java.util). TimerTask)
Such as: Perform tasks every one hours timer.schedule (timertask, 0, 60 * 60 * 1000).
The first parameter of the schedule method is the task that needs to be performed, and the type of this class is java.util.TimerTask.
The second parameter is the wait time before the task is performed, where 0 indicates no wait, and the third parameter is a time interval, in milliseconds.
Since we want the Web project to start, timer can automatically start the timer, so that throughout the lifetime of the Web project will be scheduled to perform tasks, so the start Timer class can not be a generic class, where the Servlet listener class to start the timer, by configuring this listener in the configuration file, It is automatically loaded at the start of the project, and the lifetime of the whole Web project is lifetime.
To use this listener you need to configure it in Web.xml as follows: [HTML] view plain copy <listener> <listener-class>com.demo.timer. Contextlistener</listener-class> </listener>
To implement the Javax.servlet.ServletContextListener interface with the servlet listener, the following is the class design:
the contents of the listener [Java] View plain copy package com.demo.timer; import javax.servlet.servletcontextevent; import javax.servlet.servletcontextlistener; /** * Listener content */ public class contextlistener implements servletcontextlistener { private static final long serialversionuid = 1l; public contextlistener () { } private java.util.Timer timer = null; public void contextinitialized (servletcontextevent event) { /** * set a timer */ timer = new java.util.timer (True); Event.getservletcontext (). Log ("timer started"); /** * timer to a specified time, perform an action (such as a class, or method) */ //the last parameter is the monitor cycle for the monitors, now one hour timer.schedule ( New mytask (Event.getservletcontext ()), 0, 60 * 60 * 1000); event.getservletcontext (). Log ("Added Task Scheduler table"); &NBSP;&NBSP; }