Implementing scheduled tasks using @Scheduled in Spring Boot

编写Spring Boot应用中经常会遇到这样的场景,比如:我需要定时地发送一些短信、邮件之类的操作,也可能会定时地检查和监控一些标志、参数,或者实时的显示一些数据等。
1. 添加@EnableScheduling注解到入口类声明上面,启用定时任务的配置,如下所示:
@SpringBootApplication
//在Spring Boot的主类中加入@EnableScheduling注解,启用定时任务的配置
@EnableScheduling
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

2. Add @Scheduled to the method without parameters as follows:

@RestController
@RequestMapping(value = "/SpringScheduledController")
public class SpringScheduledController {

    int i = 0;
    @Scheduled(fixedRate = 3000)
    @ApiOperation(value = "springScheduled", notes = "spring定时器")
    @RequestMapping(value = "/springScheduled", method = RequestMethod.POST)
    public void springScheduled() {
        System.out.println("----------" + i++);
    }
}

3. Running result:
write picture description here

@Scheduled parameter

@Scheduled(fixedRate = 5000) :上一次开始执行时间点之后5秒再执行
@Scheduled(fixedDelay = 5000) :上一次执行完毕时间点之后5秒再执行
@Scheduled(initialDelay=1000, fixedRate=5000) :第一次延迟1秒后执行,之后按fixedRate的规则每5秒执行一次
@Scheduled(cron="*/5 * * * * *") :通过cron表达式定义规则

Detailed explanation of cron expressions:
A cron expression has at least 6 (and possibly 7) time elements separated by spaces.
In order,
1 second (0~59)
2 minutes (0~59)
3 hours (0~23)
4 days (0~31)
May (0~11)
6 weeks (1~7 1=SUN or SUN , MON, TUE, WED, THU, FRI, SAT)
7. Year (1970-2099)
where each element can be a value (such as 6), a continuous interval (9-12), an interval (8-18 /4) (/ means every 4 hours), a list (1,3,5), wildcards. Since the two elements of "day of the month" and "day of the week" are mutually exclusive, it is necessary to set one of them?.
0 0 10,14,16 * * ? Every day at 10 am, 2 pm, 4
0 0/30 9-17 * * ? 0 0 12 every half hour during 9 to 5
working hours 10 ? * *" triggers "0 15 10 * * ?" every day at 10:15 am every day "0 15 10 * * ? *" at 10:15 am every day triggers "0 15 10 * * ? 2005" every day at 10:15 am 2005 "0*14**?" fires every day of the year at 10:15am every 1 minute between 2pm and 2:59pm






"0 0/5 14 * * ?" triggers every 5 minutes from 2pm to 2:55pm
daily "0 0/5 14,18 * * ?" from 2pm to 2:55pm daily Triggers "0 0-5 14 * * ?" every 5 minutes from 6:00 to 6:55 Triggers "0 10,44 14 ? 3 WED"
every 1 minute from 2:00 pm to 2:05 pm daily every three days "0 15 10 ? *MON-FRI"
fires at 2:10 pm and 2:44 pm on Wednesdays of the month "0 15 10 15 * ?"
fires at 10:15 am Monday-Friday 10:15 am on the 15th of every month "0 15 10 L * ?" triggers at 10:15 am on the last day of the month "0 15 10 ? * 6L" triggers at 10:15 am on the last Friday of the month "0 15 10 ? * 6L 2002-2005" 2002 "0 15 10 ? * 6#3" fires at 10:15 am on the last Friday of every month from year to 2005 Fires at 10:15 am on the third Friday of every month Some subexpressions can contain some ranges or lists For example: Subexpression (day (week)) can be "MON-FRI", "MON, WED, FRI", "MON-WED, SAT" "*" character represents all possible values ​​"/" character is used to specify the value of Increment Example : "0/15" in the subexpression (minutes) means starting from the 0th minute, every 15 minutes "3/20" in the subexpression (minutes) means starting from the 3rd minute , every 15 minutes 20 minutes (it has the same meaning as "3, 23, 43")











The "?" character is only used for the day (month) and day (week) sub-expressions, indicating that no value is specified.
When one of the two sub-expressions is assigned a value, in order to avoid conflicts, the other sub-expression needs to be replaced. The value of the formula is set to "?" The
"L" character is only used in the day (month) and day (week) subexpressions, it is an abbreviation for the word "last"
If there is specific content before "L", it has other meanings. For example: "6L" for the 6th last day of the month
Note : When using the "L" parameter, do not specify a list or range as this can cause problems The
W character represents a weekday (Mon-Fri) and can only be used for in the day domain. It is used to specify the nearest weekday to the specified day. Most business processing is workweek based, so the W character can be very important.
For example, 15W in the day field means "the closest weekday to the 15th of the month." If the 15th is a Saturday, the trigger will fire on the 14th (Friday), because Thursday is closer to the 15th than Monday.
C: means "Calendar". It means the date to which the plan is associated, or if the date is not associated, it is equivalent to all dates in the calendar. For example, 5C in the date field is equivalent to the first day after the 5th in the calendar. 1C is equivalent to the first day after Sunday in the day of the week field.

   字段     允许值                 允许的特殊字符
   秒       0-59                 , - * /
   分       0-59                 , - * /
   小时     0-23                 , - * /
   日期     1-31                 , - * ? / L W C
   月份     1-12 或者 JAN-DEC     , - * /
   星期     1-7 或者 SUN-SAT       , - * ? / L C #
   年(可选)    留空, 1970-2099     , - * /

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325648646&siteId=291194637