如何在 Spring Boot 中启用定时任务

添加 @EnableScheduling 注解

可以添加在 Application 类上

java 复制代码
@SpringBootApplication
@EnableScheduling
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

定义定时任务类和方法

定时任务类要注册为Spring IoC 容器的 Bean,或者通过 @Bean 将某个方法的返回值(返回值为定时任务类对象)注册为 Spring IoC 的Bean。

java 复制代码
package com.cmcc.nlpt.sitemessage.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledTask {

    /**
     * 每日0点执行
     */
    @Scheduled (cron = "0 0 0 * * ?")
    public void doCronTask() {

    }

    /**
     * 本次任务结束到下次任务开始的时间间隔为5秒
     */
    @Scheduled (fixedDelay = 5000)
    public void doFixedDelayTask() {

    }

    /**
     * 两次任务开始时间间间隔为5秒
     */
    @Scheduled(fixedRate = 5000)
    public void doFixedRateTask() {

    }

    /**
     * 第一次任务启动延时1秒,后续任务结束到下次任务开始的时间间隔为5秒
     */
    @Scheduled(initialDelay = 1000, fixedDelay = 5000)
    public void doInitialDelayTask() {

    }
}

通过配置项控制定时任务是否开启

方法一 @ConditionalOnProperty

此方法是控制任务类是否注册为 Spring IoC 的 Bean,可以控制上述所有类型的定时任务是否注册。

yaml 复制代码
# enabled 配置为 false 时定时任务就不会开启
scheduled-task:
  enabled: true
java 复制代码
@Component
@ConditionalOnProperty(name = "scheduled-task.enabled", havingValue = "true")
public class ScheduledTask {
    /**
     * 每日0点执行
     */
    @Scheduled (cron = "0 0 0 * * ?")
    public void doCronTask() {

    }

    /**
     * 本次任务结束到下次任务开始的时间间隔为5秒
     */
    @Scheduled (fixedDelay = 5000)
    public void doFixedDelayTask() {

    }

    /**
     * 两次任务开始时间间间隔为5秒
     */
    @Scheduled(fixedRate = 5000)
    public void doFixedRateTask() {

    }

    /**
     * 第一次任务启动延时1秒,后续任务结束到下次任务开始的时间间隔为5秒
     */
    @Scheduled(initialDelay = 1000, fixedDelay = 5000)
    public void doInitialDelayTask() {

    }
}

方法二 Boolean 标志位

使用此方法定时任务还是会被调度执行,只是在任务执行时什么都不做,也可以控制上述所有类型的定时任务。

yaml 复制代码
scheduled-task:
  enabled: true
java 复制代码
@Component
@ConditionalOnProperty(name = "scheduled-task.enabled", havingValue = "true")
public class ScheduledTask {

    @Value("${scheduled-task.enabled}")
    private Boolean taskEnabled;

    @Scheduled (fixedDelay = 5000)
    public void doFixedDelayTask() {
        if (!taskEnabled) {
            return;
        }
        System.out.println("start to doFixedDelayTask");
    }
}

使用"-"使 cron 定时任务关闭

此方法只适用于 cron 类型的定时任务,不适用于其它类型的定时任务。

注意:yml 文件中配置 - 时要加上双引号或单引号,否则会报错。


关闭 cron 定时任务的配置:

yaml 复制代码
scheduled-task:
  cron: "-"

开始 cron 定时任务的配置,将 cron 表达式配置上即可:

yaml 复制代码
scheduled-task:
  cron: 0 0 0 * * ?
java 复制代码
@Component
public class ScheduledTask {
    /**
     * 每日0点执行
     */
    @Scheduled (cron = "${scheduled-task.cron}")
    public void doCronTask() {

    }
}

参考

相关推荐
q***71014 小时前
Spring Boot(快速上手)
java·spring boot·后端
n***84074 小时前
十七:Spring Boot依赖 (2)-- spring-boot-starter-web 依赖详解
前端·spring boot·后端
爱学习的小可爱卢7 小时前
JavaEE进阶——SpringMVC响应处理详解
spring boot·postman·javaee
7***68438 小时前
Spring Boot 从 2.7.x 升级到 3.3注意事项
数据库·hive·spring boot
L***d6708 小时前
Spring Boot 各种事务操作实战(自动回滚、手动回滚、部分回滚)
java·数据库·spring boot
凌波粒8 小时前
Springboot基础教程(3)--自动装配原理/静态资源处理/欢迎页
java·spring boot·后端
凌波粒8 小时前
SpringBoot基础教程(2)--yaml/配置文件注入/数据校验/多环境配置
java·spring boot·后端·spring
S***26758 小时前
Spring Boot环境配置
java·spring boot·后端
6***83058 小时前
什么是Spring Boot 应用开发?
java·spring boot·后端
n***78688 小时前
SpringBoot详解
java·spring boot·后端