胖胖的枫叶
主页
博客
知识图谱
产品设计
数据分析
企业架构
项目管理
效率工具
全栈开发
后端
前端
测试
运维
数据
面试
  • openJdk-docs
  • spring-projects-docs
  • mysql-docs
  • redis-commands
  • redis-projects
  • apache-rocketmq
  • docker-docs
  • mybatis-docs
  • netty-docs
  • journaldev
  • geeksforgeeks
  • 浮生若梦
  • 后端进阶
  • 并发编程网
  • 英语肌肉记忆锻炼软件
  • 墨菲安全
  • Redisson-docs
  • jmh-Visual
  • 美团技术
  • MavenSearch
主页
博客
知识图谱
产品设计
数据分析
企业架构
项目管理
效率工具
全栈开发
后端
前端
测试
运维
数据
面试
  • openJdk-docs
  • spring-projects-docs
  • mysql-docs
  • redis-commands
  • redis-projects
  • apache-rocketmq
  • docker-docs
  • mybatis-docs
  • netty-docs
  • journaldev
  • geeksforgeeks
  • 浮生若梦
  • 后端进阶
  • 并发编程网
  • 英语肌肉记忆锻炼软件
  • 墨菲安全
  • Redisson-docs
  • jmh-Visual
  • 美团技术
  • MavenSearch
  • 博客

    • 博客迁移说明
    • 2024年
    • 2023年
    • 2022年
    • 2021年
    • 2020年
    • 2019年
    • 2018年

Scheduling-Tasks

Spring Scheduler里有两个概念:任务(Task)和运行任务的框架(TaskExecutor/TaskScheduler)。TaskExecutor顾名思义,是任务的执行器,允许我们异步执行多个任务。TaskScheduler是任务调度器,来运行未来的定时任务。触发器Trigger可以决定定时任务是否该运行了,最常用的触发器是CronTrigger,具体用法会在下面章节中详细介绍。Spring内置了多种类型的TaskExecutor和TaskScheduler,方便用户根据不同业务场景选择。

  • 相同的方案还有Quartz,但是需要持久化数据库。
  • Java.util中的Timer,功能相对少了点。

@Scheduled

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {

	/**
	 * cron 表达式
	 */
	String cron() default "";

	/**
	 * 将解析 cron 表达式的时区。默认情况下,此属性为空字符串(即将使用服务器的本地时区
	 */
	String zone() default "";

	/**
	 * Execute the annotated method with a fixed period in milliseconds between the
	 */
	long fixedDelay() default -1;

	/**
	 * 和fixedDelay一个意思,只是类型是字符串。
	 */
	String fixedDelayString() default "";

	/**
	 * 表示按照一定频率时间间隔执行,类型long单位ms。
	 */
	long fixedRate() default -1;

	/**
	 * 和fixedRate一个意思,只是类型是字符串。
	 */
	String fixedRateString() default "";

	/**
	 * 表示延时多久再执行第一次任务,类型long单位ms。
	 */
	long initialDelay() default -1;

	/** 
	 * 和initialDelay一个意思,只是类型是字符串。
	 */
	String initialDelayString() default "";

}

Cron表示式

  • cron生成
 ┌───────────── second (0-59)
 │ ┌───────────── minute (0 - 59)
 │ │ ┌───────────── hour (0 - 23)
 │ │ │ ┌───────────── day of the month (1 - 31)
 │ │ │ │ ┌───────────── month (1 - 12) (or JAN-DEC)
 │ │ │ │ │ ┌───────────── day of the week (0 - 7)
 │ │ │ │ │ │          (0 or 7 is Sunday, or MON-SUN)
 │ │ │ │ │ │
 * * * * * *

每一个域可出现的字符如下:
Seconds:可出现", - * /"四个字符,有效范围为0-59的整数
Minutes:可出现", - * /"四个字符,有效范围为0-59的整数
Hours:可出现", - * /"四个字符,有效范围为0-23的整数
DayofMonth:可出现", - * / ? L W C"八个字符,有效范围为0-31的整数
Month:可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEc
DayofWeek:可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推
Year:可出现", - * /"四个字符,有效范围为1970-2099年

举几个例子:
0 0 2 1 * ? * 表示在每月的1日的凌晨2点调度任务
0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业
0 15 10 ? 6L 2020-2021 表示2020-2021年的每个月的最后一个星期五上午10:15执行作

演示代码

/**
 * @author z201.coding@gmail.com
 **/
@Component
@Slf4j
public class ScheduleTasks {

    private final static String DATE_PATTERN_S = "yyyy-MM-dd HH:mm:ss";

    private  String conversionNowFormat() {
        Instant instant = Instant.ofEpochMilli(currentTimeMillis());
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(DATE_PATTERN_S));
    }

    private  Long currentTimeMillis() {
        return Clock.systemDefaultZone().millis();
    }

    @Scheduled(fixedDelay = 60000) // fixedDelay = 60000 表示當前方法執行完成 60000ms(1分钟) 后,Spring scheduling会再次执行方法
    public void testFixDelay() {
        log.info("fixedDelay: 时间 :{}",conversionNowFormat());
    }

    @Scheduled(fixedRate = 60000) // fixedRate = 60000 表示当前方法开始执行 60000ms(1分钟) 后,Spring scheduling会再次执行方法
    public void testFixedRate() {
        log.info("fixedRate: 时间 :{}", conversionNowFormat());
    }

    @Scheduled(initialDelay = 180000, fixedRate = 5000)
    // initialDelay = 180000 表示延迟 180000 (3秒) 执行第一次方法, 然後每 5000ms(5 秒) 再次执行方法
    public void testInitialDelay() {
        log.info("initialDelay: 时间 :{}", conversionNowFormat());
    }

    @Scheduled(cron = "0 0/1 * * * ?") // cron接受cron表示式,根据cron表示式规则执行
    public void testCron() {
        log.info("cron: 时间:{}", conversionNowFormat());
    }
}


  • Console
[main] [Version.java : 52] JBoss Threads version 3.1.0.Final
[main] [UndertowWebServer.java : 133] Undertow started on port(s) 9032 (http)
[scheduling-1] [ScheduleTasks.java : 38] fixedRate: 时间 :2022-02-09 15:52:48
[scheduling-1] [ScheduleTasks.java : 33] fixedDelay: 时间 :2022-02-09 15:52:48
[main] [StartupInfoLogger.java : 61] Started AppApplication in 1.67 seconds (JVM running for 2.326)
[scheduling-1] [ScheduleTasks.java : 49] cron: 时间:2022-02-09 15:53:00
  • 项目还没启动完成就开始执行了。
Last Updated:
Contributors: 庆峰