如何在 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() {

    }
}

参考

相关推荐
m0_7482386312 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
菜鸟阿达1 小时前
spring boot 2.7 + seata +微服务 降级失败问题修复
spring boot·后端·微服务
張葒兵3 小时前
记一次命令行启动springboot项目的问题 java -jar的问题
java·spring boot·jar
mah66610 小时前
SpringBoot项目连接Oracle视图报错整理
spring boot·后端·oracle
IT学长编程11 小时前
计算机毕业设计 基于SpringBoot的智慧社区管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·后端·毕业设计·课程设计·论文笔记·1024程序员节
奋斗的小方11 小时前
Springboot基础篇(3):Bean管理
java·spring boot·后端
扣丁梦想家12 小时前
《Spring Boot + MySQL高性能应用实战:性能优化技巧与最佳实践》
spring boot·后端·mysql
Code额13 小时前
Springboot 自动化装配的原理
java·spring boot
夏天的味道٥13 小时前
36. Spring Boot 2.1.3.RELEASE 中实现监控信息可视化并添加邮件报警功能
java·spring boot·后端
计算机学长felix14 小时前
基于SpringBoot的“洪涝灾害应急信息管理系统”的设计与实现(源码+数据库+文档+PPT)
spring boot·毕业设计