定时任务-spring整合cron表达式

发布于 2023-01-11  127 次阅读


文档

Cron Trigger Tutorial

1.实现方案

1.1.Timer

1.2.线程池

1.3.mq的延时队列

1.4.QUARTZ

1.搭配cron表达式使用 2.支持年,spring不支持年 3.在周几的位置,quartz的周1是2,spring的周1是1

spring框架的定时任务

springboot默认定时任务框架不是QUARTZ,如果需要使用引入即可

2.cron表达式

2.1. 语法:

秒 分 时 日 月 周 年 (spring 不支持年,所以可以不写)

http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html

A cron expression is a string comprised of 6 or 7 fields separated by white space. Fields can contain any of the allowed values, along with various combinations of the allowed special characters for that field. The fields are as follows:

Field NameMandatoryAllowed ValuesAllowed Special Characters
SecondsYES0-59, - * /
MinutesYES0-59, - * /
HoursYES0-23, - * /
Day of monthYES1-31, - * ? / L W
MonthYES1-12 or JAN-DEC, - * /
Day of weekYES1-7 or SUN-SAT(1:周末 7:周一), - * ? / L #
YearNOempty, 1970-2099, - * /

特殊字符:

  • , :枚举;(cron="7,9,23****?") : 任意时刻的7,9,23秒启动这个任务;
  • -:范围:(cron="7-20****?""):任意时刻的7-20秒之间,每秒启动一次
  • *:任意;
  • /:步长;(cron="7/5****?"):第7秒启动,每5秒一次; (cron="*/5****?"):任意秒启动,每5秒一次;
  • ?:(出现在日和周几的位置):为了防止日和周冲突,在周和日上如果要写通配符使用?(cron="***1*?"):每月的1号,而且必须是周二然后启动这个任务;
  • L:(出现在日和周的位置)”,last:最后一个(cron="***?*3L"):每月的最后一个周二(1:周末 2:周一 3:周二 4:周三 5:周四 6:周五 7:周六)
  • W:Work Day:工作日(cron="***W*?"):每个月的工作日触发 (cron="***LW*?"):每个月的最后一个工作日触发
  • #:第几个(cron="***?*5#2"):每个月的 第2个周4

2.2. 示例:

ExpressionMeaning
0 0 12 * * ?Fire at 12pm (noon) every day
0 15 10 ? * *Fire at 10:15am every day
0 15 10 * * ?Fire at 10:15am every day
0 15 10 * * ? *Fire at 10:15am every day
0 15 10 * * ? 2005Fire at 10:15am every day during the year 2005
0 * 14 * * ?Fire every minute starting at 2pm and ending at 2:59pm, every day
0 0/5 14 * * ?Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day
0 0/5 14,18 * * ?Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day
0 0-5 14 * * ?Fire every minute starting at 2pm and ending at 2:05pm, every day
0 10,44 14 ? 3 WEDFire at 2:10pm and at 2:44pm every Wednesday in the month of March.
0 15 10 ? * MON-FRIFire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
0 15 10 15 * ?Fire at 10:15am on the 15th day of every month
0 15 10 L * ?Fire at 10:15am on the last day of every month
0 15 10 L-2 * ?Fire at 10:15am on the 2nd-to-last last day of every month
0 15 10 ? * 6LFire at 10:15am on the last Friday of every month
0 15 10 ? * 6LFire at 10:15am on the last Friday of every month
0 15 10 ? * 6L 2002-2005Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005
0 15 10 ? * 6#3Fire at 10:15am on the third Friday of every month
0 0 12 1/5 * ?Fire at 12pm (noon) every 5 days every month, starting on the first day of the month.
0 11 11 11 11 ?Fire every November 11th at 11:11am.

2.3.在线Cron表达式生成器

在线Cron表达式生成器

3.SpringBoot整合

3.1 开启定时任务

整合步骤:

  1. @EnableScheduling【spring 默认是使用自己的定时任务,如果想整合Quartz,参考官方】
  2. @Scheduled
  3. 定时任务配置类:TaskSchedulingAutoConfiguration
@Slf4j
@Component
@EnableScheduling
public class HelloScheduled {
   
   /**
    * 周二的任意秒启动,每隔五秒执行一次
    * 1、spring中不支持第七位(年)
    * 2、sping中1-7,分别代表周一到周七
    */
   @Scheduled(cron = "*/5 * * ? * 2")
   public void hello() {
       log.info("hello...");
  }
   
}

3.2 定时任务+异步任务

定时任务默认情况排队执行,所以前一个任务超时后面任务也会超时

解决方案: 方案1:业务方法自己作异步编排【CompletableFuture.runAsync】 方案2:修改定时任务线程池的线程个数(默认为1)【spring.task.scheduling.pool.size=5】(部分版本有bug,不一定生效) 方案3:让定时任务异步执行

方案3:让定时任务异步执行 整合步骤:

  1. @EnableAsync
  2. @Async
  3. 异步配置类:TaskExecutionAutoConfiguration
    • 配置线程池:
      • 核心线程数spring.task.execution.pool.core-size=20
      • 最大线程数 spring.task.execution.pool.max-size=50
@Slf4j
@Component
@EnableAsync// 普通service方法也可以标注异步执行
@EnableScheduling
public class HelloScheduled {
   
   @Async
   @Scheduled(cron = "* * * ? * 2")// 周二的任意秒启动,每秒执行一次
   public void hello() throws InterruptedException {
       log.info("hello...");
       Thread.sleep(3000);
  }
}