The use of @Schedule annotation for timing tasks in SpringBoot

1 Overview

The @Scheduled annotation is an annotation provided by spring boot for timing task control. It is mainly used to control the execution of tasks at a specified time, or at regular intervals. Note that it needs to be used in conjunction with @EnableScheduling. There are three main configurations for @Scheduled configuration execution Time mode, cron, fixedRate, fixedDelay. Its configuration has a total of 8 parameters.

(After being initialized by spring, this scheduled task will start to execute, and the following annotations such as cron, fixedDelay, and fixedRate are all like this.)

2. Parameter explanation

2.1 cron expression

This parameter accepts a cron expression. The cron expression is a string separated by 5 or 6 spaces and separated into 6 or 7 fields. Each field represents a meaning.
cron expression syntax

[秒] [分] [小时] [日] [月] [周] [年]

Note: [Year] is not a required field, you can omit [Year], then there are 6 fields in total

serial number illustrate required Allowed values wildcards allowed
1 Second yes 0-59 , - * /
2 point yes 0-59 , - * /
3 hour yes 0-23 , - * /
4 Day yes 1-31 , - * ? / L W
5 moon yes 1-12 / JAN-DEC , - * /
6 week yes 1-7 or SUN-SAT , - * ? / L #
7 Year no 1970-2099 , - * /

Wildcard description:

* 表示所有值。 例如:在分的字段上设置 *,表示每一分钟都会触发。
? 表示不指定值。使用的场景为不需要关心当前设置这个字段的值。例如:要在每月的10号触发一个操作,但不关心是周几,所以需要周位置的那个字段设置为”?” 具体设置为 0 0 0 10 * ?
- 表示区间。例如 在小时上设置 “10-12”,表示 10,11,12点都会触发。
, 表示指定多个值,例如在周字段上设置 “MON,WED,FRI” 表示周一,周三和周五触发
/ 用于递增触发。如在秒上面设置”5/15” 表示从5秒开始,每增15秒触发(5,20,35,50)。 在日字段上设置’1/3’所示每月1号开始,每隔三天触发一次。
L 表示最后的意思。在日字段设置上,表示当月的最后一天(依据当前月份,如果是二月还会依据是否是润年[leap]), 在周字段上表示星期六,相当于”7”或”SAT”。如果在”L”前加上数字,则表示该数据的最后一个。例如在周字段上设置”6L”这样的格式,则表示“本月最后一个星期五”
W 表示离指定日期的最近那个工作日(周一至周五). 例如在日字段上置”15W”,表示离每月15号最近的那个工作日触发。如果15号正好是周六,则找最近的周五(14号)触发, 如果15号是周未,则找最近的下周一(16号)触发.如果15号正好在工作日(周一至周五),则就在该天触发。如果指定格式为 “1W”,它则表示每月1号往后最近的工作日触发。如果1号正是周六,则将在3号下周一触发。(注,”W”前只能设置具体的数字,不允许区间”-“)。
# 序号(表示每月的第几个周几),例如在周字段上设置”6#3”表示在每月的第三个周六.注意如果指定”#5”,正好第五周没有周六,则不会触发该配置(用在母亲节和父亲节再合适不过了) ;小提示:’L’和 ‘W’可以一组合使用。如果在日字段上设置”LW”,则表示在本月的最后一个工作日触发;周字段的设置,若使用英文字母是不区分大小写的,即MON与mon相同。

example

  • Execute every 5 seconds: /5 * ?
  • Execute every 1 minute: 0 /1 ?
  • Execute once every day at 23 o'clock: 0 0 23 ?
  • Execute once every day at 1 am: 0 0 1 ?
  • Execute once at 1 am on the 1st of every month: 0 0 1 1 * ?
  • Execute once at 23:00 on the last day of each month: 0 0 23 L * ?
  • Executed every Saturday at 1 am: 0 0 1 ? * L
  • Execute once at 26 minutes, 29 minutes, and 33 minutes: 0 26,29,33 * ?
  • Execute once every day at 0:00, 13:00, 18:00, and 21:00: 0 0 0,13,18,21 ?

cron expressions use placeholders
In addition, the cron expressions received by the cron attribute support placeholders. eg:
configuration file:

time:
  cron: */5 * * * * *
  interval: 5

Execute every 5 seconds:

@Scheduled(cron="${time.cron}")
    void testPlaceholder1() {
    
    
        System.out.println("Execute at " + System.currentTimeMillis());
    }

    @Scheduled(cron="*/${time.interval} * * * * *")
    void testPlaceholder2() {
    
    
        System.out.println("Execute at " + System.currentTimeMillis());
    }

2.2. zone

Time zone, accepts a java.util.TimeZone#ID. cron expressions will be parsed based on this time zone. The default is an empty string, that is, the time zone where the server is located. For example, the time zone we generally use is Asia/Shanghai. We generally leave this field blank.

2.3. fixedDelay

How long to execute after the last execution time point. like:

@Scheduled(fixedDelay = 5000) //上一次执行完毕时间点之后5秒再执行

2.4. fixedDelayString

It has the same meaning as 3. fixedDelay, but in the form of a string. The only difference is support for placeholders. like:

@Scheduled(fixedDelayString = "5000") //上一次执行完毕时间点之后5秒再执行

The use of placeholders (configuration in the configuration file: time.fixedDelay=5000):

@Scheduled(fixedDelayString = "${time.fixedDelay}")
    void testFixedDelayString() {
    
    
        System.out.println("Execute at " + System.currentTimeMillis());
    }

operation result

2.5. fixedRate

How long to execute after the last execution time point. like:

@Scheduled(fixedRate = 5000) //上一次开始执行时间点之后5秒再执行

2.6. fixedRateString

It has the same meaning as 5. fixedRate, only in the form of a string. The only difference is support for placeholders.

2.7. initialDelay

How long to execute after the first delay. like:

@Scheduled(initialDelay=1000, fixedRate=5000) //第一次延迟1秒后执行,之后按fixedRate的规则每5秒执行一次

2.8. initialDelayString

It has the same meaning as 7. initialDelay, but in the form of a string. The only difference is support for placeholders.

3 Small test results of three execution mechanisms (refer to the article)

1. fixedRate configures the interval from the start time of the previous task to the start time of the next task, and each task will be executed;

2. fixedDelay configures the interval between the end time of the previous task and the start time of the next task, and each task will be executed;

3. When the cron expression is configured to execute the task, it will judge whether the task can be executed at the configured start time of the task. If it can be executed, it will skip this execution;

4. If it is a scheduled task that emphasizes the task interval, it is recommended to use fixedRate and fixedDelay. If it is a scheduled task that emphasizes that the task will be executed at a certain time, it is recommended to use cron expressions.

Guess you like

Origin blog.csdn.net/qq_27480007/article/details/130133486