Timed task @Scheduled annotation learning

The @Scheduled annotation is provided in Spring to facilitate the development of scheduled tasks.

To use the @Scheduled annotation, you first need to add @EnableScheduling to the startup class to enable Spring's scheduled task execution function, so that you can detect the Scheduled annotation on any Spring-managed bean in the container and execute the scheduled task.

@ EnableScheduling  is used on the configuration class to enable the support of scheduled tasks (on the class)

@Scheduled to declare that this is a task, including cron, fixDelay, fixRate and other types (in terms of method, you need to enable the support of scheduled tasks first)

 Annotation source code:

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {
    
    String CRON_DISABLED = "-";

	String cron() default "";

	String zone() default "";

	long fixedDelay() default -1L;

	String fixedDelayString() default "";

	long fixedRate() default -1L;

	String fixedRateString() default "";
	
	long initialDelay() default -1L;

	String initialDelayString() default "";

}

Parameter Description:

parameter Parameter Description example
cron cron expression for task execution 0/1 * * * * ?
zone The time zone used for cron expression parsing, the default is the local time zone of the server, use java.util.TimeZone#getTimeZone(String) method to parse GMT-8:00
fixedDelay The interval between the end of the last task execution and the start of the next execution, in ms 1000

fixedDelayString

The interval between the end of the last task execution and the start of the next execution, parsed using java.time.Duration#parse PT15M
fixedRate Execute tasks at fixed intervals, that is, the interval between the start of the previous task execution and the start of the next execution. The unit is ms. If the last task has not been completed when scheduling task execution, it will join the worker queue and wait for the last execution to complete. Execute the next task immediately after 2000
fixedRateString Consistent with fixedRate logic, just use java.time.Duration#parse to parse PT15M

initialDelay

The delay time of the first task execution 1000
initialDelayString The delay time of the first task execution, parsed using java.time.Duration#parse PT15M

fixedRate is to configure the time interval from the start of the last task execution to the start of the next execution. It will schedule the next task without waiting for the completion of the previous task execution and put it in the waiting queue.

fixedDelay is the configured interval between the end of the last task execution and the start of the next execution, that is to say, it will wait for the end of the last task execution, delay the interval time, and then execute the next task.

 some timing examples

cron 表达式格式:
{秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)}

# 每隔5秒执行一次
@Scheduled(cron = "*/5 * * * * ?")
# 每隔1分钟执行一次
@Scheduled(cron = "0 */1 * * * ?")
# 每天23点执行一次
@Scheduled(cron = "0 0 23 * * ?")
# 每天凌晨1点执行一次
@Scheduled(cron = "0 0 1 * * ?")
# 每月1号凌晨1点执行一次
@Scheduled(cron = "0 0 1 1 * ?")
# 每月最后一天23点执行一次;
@Scheduled(cron = "0 0 23 L * ?")
# 每周星期天凌晨1点执行一次
@Scheduled(cron = "0 0 1 ? * L")
# 在26分、29分、33分执行一次
@Scheduled(cron = "0 26,29,33 * * * ?")
# 每天的0点、13点、18点、21点都执行一次
@Scheduled(crom = "0 0 0,13,18,21 * * ?")

cron expression

A cron expression consists of 7 parts, each part is separated by a space. The meanings of the 7 parts of a cron expression from left to right are as follows:

Second Minute Hour Day Month Week (Year)

where year is optional.

Field Name Allowed Values ​​Allowed Special Characters
Seconds 0-59 , - * /
Minutes 0-59 , - * /
Hours 0-23 , - * /
Days 1-31 , - * ? / LWC
Months 1-12 or JAN- DEC , - * /
week 1-7 or SUN-SAT , - * ? / LC #
year (optional field) empty, 1970-2099 , - * /

Description of special characters

  • , : Indicates to list the enumerated values, for example, if you use 5,20 in minutes, it means that it will be triggered every minute at 5 and 20 minutes.
  • - : Indicates the range. For example, use 5-20 in minutes, which means triggering once every minute from 5 minutes to 20 minutes.
  • ***** : Indicates to match any value of the field. If * is used in the domain, it means that the event will be triggered every minute.
  • / : Indicates that the trigger starts at the starting time, and then triggers every fixed time. For example, if 5/20 is used in the domain, it means that it is triggered once every 5 minutes, and it is triggered once every 25, 45, etc.
  • ? : Can only be used in week and day. It also matches any value of the domain, but it doesn't because the day of the week and the day of the week affect each other. For example, if you want to trigger scheduling on the 20th of each month, no matter what day of the week the 20th is, you can only use the following writing: 13 13 15 20 * ?, the last digit can only be used? , instead of using *, if you use * to indicate that it will be triggered regardless of the day of the week, it is not the case.
  • L: Indicates that at the end, only the day and week can appear. If 5L is used on the day, it means that it will be triggered on the last Thursday.
  • W : Indicates a valid working day (Monday to Friday), which can only appear in the week field, and the system will divide the event on the nearest valid working day to the specified date. For example: use 5W on a day, if the 5th is a Saturday, it will be triggered on the nearest working day: Friday, that is, the 4th. If the 5th is Sunday, it will be triggered on the 6th; if the 5th falls on a day from Monday to Friday, it will be triggered on the 5th. Another point, W's most recent lookup does not span months.
  • # : Used to determine the day of the week of each month, it can only appear in the week. For example, in 4#2, it means the second Wednesday of a certain month.
  • LW: These two characters can be used together to indicate the last working day of a certain month, that is, the last Friday.

————————————————
Copyright statement: This article is the original article of CSDN blogger "Bai Juyi." It follows the CC 4.0 BY-SA copyright agreement. For reprinting, please attach the original source link and this statement.
Original link: https://blog.csdn.net/weixin_45866849/article/details/123530483

Guess you like

Origin blog.csdn.net/mao_mao37/article/details/129935136