spring boot timing task

spring boot comes defined Task Manager


Serial execution steps
===================== 
1. Add notes @EnableScheduling on Applicaton
2. Create a new class UserTask, add annotations @Component
3. Add the following in the Task class, 
@Scheduled(cron = "*/10 * * * * ?")
public void test()
{
    Thread current = Thread.currentThread();
 System.out.println ( "Time:" + new Date () + ", the thread number:" + current.getId () + ", the thread name" + current.getName ());
}

 

Parallel step 

=====================

1. Add notes @EnableAsync on Applicaton

2. UserTask, add annotations @Async

3. Config class increases, as follows:

@Configuration
public class TaskConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
    }
}

 

 

 

cron expression

===================== 

 

cron expressions, a special syntax, and it feels a little bit about the people, but simply, we can remember some common uses, special syntax can be used alone to investigate.

 

cron a total of seven, but the last one year, it can be left blank, so we can write 6:

The first one, in seconds, the value 0-59
Second, representing minutes, the value 0-59
Third place, the hour, the value of 0-23
Fourth place, date, day / date, the value of 1-31
Fifth, day of the month, the value of 1-12
Sixth, a week, a string of 1-7, Monday, Tuesday ... Note: not the first week, second week another meaning: 1 for Sunday, 2 for Monday.
Seventh, year, you can be left blank, the value 1970-2099

cron, there are some special symbols have the following meanings:

 

(*) Asterisk: each can be understood as meaning, every second, every minute, every day, every month, every year ...

 

Question mark (?): Question mark can only appear in the two weeks of the date and location, this position represents the value of uncertainty, 3:00 perform every day, so the sixth week of the location, we do not need to concern it is uncertain value. At the same time: date and day are two mutually exclusive elements, by question marks to indicate no value. For example, January 10, for example, is 1 week, if another location is specified in weeks Tuesday on before and after the conflict contradictory.
(-) minus: expression of a range, such as using "10-12" in the hour field, represents from 10 to 12, 10, 11, i.e.,
(,) Comma: the expression of a list of values, such as using "1,2,4" in the week field, said Monday, Tuesday, Thursday
(/) Slash: such as: x / y, x is the start value, y is the step size, for example, in the first (second) is 0/15, 0 seconds, every 15 seconds, the last is 0,15, another 30,45,60: * / y, equivalent to 0 / y

Schedule:

 

"0012 * *?" 12:00 every day trigger 

 

"01510? * *" Trigger every day at 10:15 am 
"01510 * *?" Trigger every day at 10:15 am 
"01510 * *? *" Trigger every day at 10:15 am 
"01510 * *? 2005" in 2005 triggered daily at 10:15 am 
"0 * 14 * *?" During the 2:00 pm to 2:59 pm every day of every 1 minute when the 
Every 5 minutes triggering "0 0/5 14 * *?" During a day at 2:00 pm to 2:55 pm 
Every 5 minutes triggering "0 0/5 * 14, 18 *?" And 6:00 to 6:55 pm during the period of 2:00 to 2:55 every afternoon 
Every 1 minute when the "00-514 * *?" During a day at 2:00 pm to 2:05 pm 
"0 10,44 14? ​​3 WED" March of the year Wednesday afternoon 2:10 and 2:44 trigger 
"0 15 10? * MON-FRI" Monday to Friday 10:15 am to trigger 
"0151015 *?" Trigger 15th of each month at 10:15 am 
"0 15 10 L *?" On the last day of each month at 10:15 am triggering 
"0 15 10? * 6L" the last Friday of each month at 10:15 am triggering 
"0 15 10? * 6L 2002-2005" 2002 years to 2005, the last Friday of each month at 10:15 am triggering
"01510? * 6 # 3" is the third Friday of each month at 10:15 am triggering 
Executed once every 5 seconds: * / 5 * * * *?
Executed once every minute: 0 * / 1 * * *?
23:00 executed once a day: 0023 * *?
1:00 executed once a day: 001 * *?
Monthly No. 1 1:00 execution time: 0011 *?
The last day of each month 23:00 executed once: 0 0 23 L *?
Weekly Sunday 1:00 to implement once: 0 0 1 * L?
 

 

Guess you like

Origin www.cnblogs.com/liucuiqiang/p/11612607.html