SpringBoot设置动态定时任务

一、背景

在不借助Nacos、Apollo等配置中心情况下,实现动态修改定时任务执行时间

二、实现

1、配置文件
#每5秒钟执行一次任务
print.time.cron=0/5 * * * * ?
2、配置类
package com.schedule.demo.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhangy
 * @date 2022/10/12 21:05
 */
@Data
@Configuration
public class ScheduleCronProperties {

    @Value("${print.time.cron}")
    private String cron;
}

3、定时任务配置类
package com.schedule.demo.task;

import com.schedule.demo.config.ScheduleCronProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 动态定时任务,可即时修改执行时间
 *
 * @author zhangy
 * @date 2022/10/12 21:04
 */
@Component
public class DynamicScheduleTask implements SchedulingConfigurer {
    private static final Logger LOGGER = LoggerFactory.getLogger(DynamicScheduleTask.class);

    @Autowired
    private ScheduleCronProperties cronProperties;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        // 动态使用cron表达式设置循环间隔
        taskRegistrar.addTriggerTask(

                // 需要执行的任务
                () -> LOGGER.info("Now time:{}", LocalDateTime.now()),

                // 传入定时任务触发器的上下文,构建一个新的cron触发器对象,并获取下次执行的时间
                triggerContext -> {
                    // 使用CronTrigger触发器,可动态修改cron表达式来操作循环规则
                    CronTrigger cronTrigger = new CronTrigger(cronProperties.getCron());
                    // 计算出下次执行时间
                    return cronTrigger.nextExecutionTime(triggerContext);
                });
               
        LOGGER.info("init success!");
    }
}

非lambda表达式形式


    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

        // 需要执行的任务
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                LOGGER.info("Now time:{}", LocalDateTime.now());
            }
        };
        
        // 构建一个新的cron触发器对象,并获取下次执行的时间
        Trigger trigger = new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                // 使用CronTrigger触发器,可动态修改cron表达式来操作循环规则
                CronTrigger cronTrigger = new CronTrigger(cronProperties.getCron());

                // 计算出下次执行时间
                return cronTrigger.nextExecutionTime(triggerContext);
            }
        };

        taskRegistrar.addTriggerTask(runnable, trigger);
        LOGGER.info("init success!");
    }
4、修改cron表达式的接口

该接口可以的http的也可以是其他形式的,核心逻辑就是修改ScheduleCronProperties 对象的cron属性

package com.schedule.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.schedule.demo.config.ScheduleCronProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* cron表达式修改
*
* @author zhangY
* @date 2021-07-15 20:24:19
*/
@RestController
@RequestMapping("/cron")
public class CronController {
   private static final Logger LOGGER = LoggerFactory.getLogger(CronController.class);

   @Autowired
   private ScheduleCronProperties scheduleCronProperties;

   @GetMapping("/get")
   public String get() {
       return JSONObject.toJSONString(scheduleCronProperties);
   }

   @GetMapping("/update")
   public String update(String newCron) {

       String oldCron = scheduleCronProperties.getCron();
       LOGGER.info("update cron, old:[{}] new:[{}]", oldCron, newCron);
       scheduleCronProperties.setCron(newCron);

       //TODO 这里可以回写配置文件或者数据库

       return JSONObject.toJSONString(scheduleCronProperties);
   }
}

三、测试

项目启动,自动执行初始的5秒钟执行一次的任务,结果如下:

Now time:2022-10-12T22:11:35
Now time:2022-10-12T22:11:40
Now time:2022-10-12T22:11:45
Now time:2022-10-12T22:11:50
Now time:2022-10-12T22:11:55

调用接口修改cron表达式

http://127.0.0.1:8080/cron/update?newCron=0/3 * * * * ?

修改后,任务执行结果如下:

Now time:2022-10-12T22:13:25
Now time:2022-10-12T22:13:27
Now time:2022-10-12T22:13:30
Now time:2022-10-12T22:13:33
Now time:2022-10-12T22:13:36
Now time:2022-10-12T22:13:39
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值