@Scheduled comment

1 Overview

@Scheduled spring boot annotations are provided for the timing annotations job control, mainly for controlling the time to perform a specified task, or performed at regular intervals. Note that with the need to use @EnableScheduling disposed @Scheduled configured to perform three main way time, cron, fixedRate, fixedDelay.

2 cron

@Scheduled cron is a parameter, a string, separated by five spaces, allowing only six fields (note not 7, 7 being given directly), respectively, expressed in seconds, minutes, hours, days, months, week.

unit allowance Wildcards are permitted
second 0-59 , - * /
Minute 0-59 , - * /
Time 0-23 , - * /
day 1-31 , - * / ? L W
month 1-12 or JAN-DEC (upper or lower case) , - * / ?
week 1-7 or SUN-SAT (upper or lower case) , - * / ? L #

2.1 cron wildcard

symbol meaning
* All values ​​in the second field represents the second execution in the month field indicates the monthly execution
? Do not specify a value, the value is not required currently in the specified fields, such as perform but do not care about a few weeks, you can put weeks of field is set every day?
- Range, such as 0-2 seconds, is 0 seconds, 1 second, 2 seconds triggers
, A plurality of values, such as 0 seconds, 20 seconds, 25 seconds trigger, the second field to be 0,20,25
/ Incrementing the trigger, the second example provided on the 0/3 field, starting from 0 seconds, every 3 seconds trigger
L Finally, only allowed on the field day or week field, use L represents the last day of the month on the daily field use in the peripheral field 3L is the last Thursday of the month
W Only permitted for use on the date field shows the closest of the day working. Weekday refers Monday to Friday
# Allowed only in the peripheral field that indicates the month of the first few days of the week, such as 2 # 3, 3rd Tuesday of each month

2.2 cron examples

@Scheduled(cron = "0 * * * 1 SAT") //每年的1月的所有周六的所有0秒时间执行
@Scheduled(cron = "0 0 0 1 Jan ?") //每年的1月的1日的0时0分0秒执行

cron support placeholders, if there is in the configuration file

cron = 2 2 2 2 2 ?

then

@Scheduled(cron = "${cron}")

Two minutes two seconds to perform two o'clock represents the annual February 2.

3 fixedRate

3.1 fixedRate

fixedRate indicate how long after the last execution time from execution, in ms.
As

@Scheduled(fixedRate = 1000 * 5)

Since the last execution five seconds before execution.

3.2 fixedRateString

There is a similar parameter called fixedRateString, is a string, with placeholders.
As

@Scheduled(fixedRateString = "1000")

1 second since the last re-executed.
If there is a corresponding attributes in the configuration file, the attribute may be acquired with placeholders, as there is in the application.properties

interval=2000

can use

@Scheduled(fixedRateStirng="${interval}")

It represents a two second interval.

4 fixedDelay

4.1 fixedDelay

fixedDelay and fixedRate somewhat similar, but fixedRate after the last start time, fixedDelay after the end of the last time, that is to say, fixedDelay indicate how long after the last execution is finished, the unit is ms.

@Scheduled(fixedDelay = 1000 * 3600 * 12) //上一次执行完毕后半天后再次执行

4.2 fixedDelayString

Similar fixedRateString, also supports placeholders

@Scheduled(fixedDelayString = "${fixedDelay}")

5 initialDelay

5.1 initialDelay

initialDelay indicate how long the delay after the first execution unit ms, after performing in accordance with cron / fixedRate / fixedRateString rules fixedDelay / fixedDelayString specified /, you need to specify one of the rules.

@Scheduled(initialDelay=1000,fixedRate=1000) //首次运行延迟1s

5.2 initialDelayString

And initialDelay similar, but is a string, with placeholders.

@Scheduled(initialDelayString = "${initialDelay}",cron = "0 0 0 14 4 ?") 
//按照配置文件initialDelay指定的时间首次延迟,并于每年4月14日0时0分0秒执行

Guess you like

Origin www.cnblogs.com/Blueeeeeeee/p/12233449.html