Adding timing tasks to Java Web programs, here are two ways: 1. Use listener injection; 2. Use Spring annotation @Scheduled injection.
The second form is recommended.
1. Use listener injection
①: Create a listener class:
import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class TimerDataTaskListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent servletContextEvent) { new TimerManager(); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { }}②: Create a timed task class:
import java.util.Calendar;import java.util.Date;import java.util.Timer;public class TimerManager { //Time interval: 24h private static final long PERIOD_DAY = 24 * 60 * 60 * 1000; public TimerManager() { Calendar calendar = Calendar.getInstance(); //Customize the execution of calendar.set(Calendar.HOUR_OF_DAY, 12); calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.SECOND, 30); Date date = calendar.getTime(); //Time of the first time to execute the time task//If the time point of the time has passed, the execution will start at the second day if (date.before(new Date())) { date = this.addDay(date, 1); } Timer timer = new Timer(); TimerTaskService task = new TimerTaskService(); //Schedule the specified task to start repeated fixed delay execution at the specified time. timer.schedule(task, date, PERIOD_DAY); } // Increase or decrease the number of days private Date addDay(Date date, int num) { Calendar startDT = Calendar.getInstance(); startDT.setTime(date); startDT.add(Calendar.DAY_OF_MONTH, num); return startDT.getTime(); }}③: Create TimerTask class:
import java.util.TimerTask;public class TimerTaskService extends TimerTask { @Override public void run() { try { //The task logic to be executed is written here System.out.println("Insert password successfully!"); } catch (Exception e) { System.out.println("Insert password failed!"); } }}④: Register the listener in web.xml
<!-- TimerDataTaskListener Listener--> <listener> <listener-class>com.jsiqi.resume.service.TimerDataTaskListener</listener-class> </listener>
2. Inject using spring annotation
The framework for my project is Spring + SpringMVC + Mybatis
Code example:
import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Componentpublic class TimerTask { @Scheduled(cron="*/30 * * * * *") // Execute public void test(){ try { //The task logic to be executed is written here System.out.println("Insert password successfully!"); } catch (Exception e) { System.out.println("Insert password failed!"); } }}The timing time setting is as follows:
CronTrigger configuration full format is: [seconds] [minutes] [hours] [days] [months] [weeks] [years] Example: 0 0 10,14,16 * * ? 10 am every day, 2 pm, 4:0 0/30 9-17 * * ? 0 half hour during nine to five working hours 0 0 12 ? * WED indicates that every Wednesday at 12 noon "0 0 12 * * ? " 0 15 10 * * ? " 10:15 am every day triggered "0 15 10 * * ? 2005" 2005 daily trigger "0 * 14 * * ?" trigger "0 0/5 14 * * ?" trigger "0 0/5 14 * * ?" trigger "0 0/5 14,18 * * ?" trigger "0 0/5 14,18 * * ?" trigger "0 0-5 14 * * ?" trigger "0 0-5 14 * * ?" trigger "0 10,44 14 ? 3 WED" trigger "0 10 and 2:44 pm on Wednesdays of March each year 10:15 am on Monday to Friday trigger "0 15 10 15 * ?" 10:15 am on the 15th of the month "0 15 10 L * ?" 10:15 am on the last day of the month "0 15 10 ? * 6L" 10:15 am on the last Friday of the month "0 15 10 ? * 6L" 10:15 am on the last Friday of the month "0 15 10 ? * 6L 2002-2005" 10:15 am on the last Friday of the month "0 15 10 ? * 6#3" 10:15 am on the third Friday of the month
| Serial number | illustrate | Is it required | Allowed values | Allowed wildcards |
|---|---|---|---|---|
| 1 | Second | yes | 0-59 | , - * / |
| 2 | point | yes | 0-59 | , - * / |
| 3 | hour | yes | 0-23 | , - * / |
| 4 | day | yes | 1-31 | , - * ? / LW |
| 5 | moon | yes | 1-12 or JAN-DEC | , - * / |
| 6 | week | yes | 1-7 or SUN-SAT | , - * ? / LW |
| 7 | Year | no | empty or 1970-2099 | , - * / |
Wildcard description:
* means all values. For example: Set "*" on the partial field, which means that every minute will be triggered.
? means no value is specified. The scenario used is that you do not need to care about the value of this field currently set.
For example: An operation is to be triggered on the 10th of each month, but it doesn’t care about the weekly day, so the field that needs to be set to "?" is set to 0 0 0 10 *?
- Indicates interval. For example, setting "10-12" on the hour means that 10, 11, and 12 points will be triggered.
, means specifying multiple values, for example, setting "MON,WED,FRI" on the weekly field means triggering on Monday, Wednesday and Friday
/ for incremental triggering. If setting "5/15" on the second, it means that starting from 5 seconds, triggering every 15 seconds (5,20,35,50). Set '1/3' on the month field to start on the 1st of the month and trigger every three days.
L means the final meaning. On the day field setting, it indicates the last day of the month (based on the current month, if it is February, it will also be based on whether it is Runnian [leap]). On the week field, it indicates Saturday, which is equivalent to "7" or "SAT". If a number is preceded by "L", it means the last one of the data. For example, setting a format like "6L" in the weekly field means "the last Friday of the month"
W indicates the closest working day to the specified date (Monday to Friday). For example, set "15W" in the day field to indicate the closest working day to the 15th of each month. If the 15th happens to be Saturday, look for the most recent Friday (14th) trigger. If the 15th is not weekly, look for the most recent Monday (16th) trigger. If the 15th happens to be on the weekday (Monday to Friday), it will trigger on that day. If the format is specified as "1W", it indicates the most recent working day after the 1st of each month. If the 1st is Saturday, it will trigger on the 3rd next Monday. (Note, only specific numbers can be set before "W", and intervals "-" are not allowed).
# Serial number (represents the week of each month), for example, setting "6#3" in the week field indicates the third Saturday of each month. Note that if "#5" is specified, it happens that there is no Saturday in the fifth week, the configuration will not be triggered (it is most suitable for Mother's Day and Father's Day);
Tips:
'L' and 'W' can be used in combination. If "LW" is set on the day field, it means it is triggered on the last working day of the month;
The setting of the week field is case-insensitive if the English letters are used, that is, MON is the same as mon;
refer to:
https://www.cnblogs.com/liaojie970/p/5913272.html
http://prisonbreak.iteye.com/blog/2247216