Spring动态添加定时任务

一、背景

在工作中,有些时候我们有些定时任务的执行可能是需要动态修改的,比如: 生成报表,有些项目配置每天的8点生成,有些项目配置每天的10点生成,像这种动态的任务执行时间,在不考虑分布式执行的情况下,我们可以
使用 Spring Task来简单的实现。

二、需求和实现思路

1、能够动态的添加一个定时任务。

Spring中存在一个类ThreadPoolTaskScheduler,它可以实现根据一个cron表达式来调度一个任务,并返回一个ScheduledFuture对象。
可以看到返回值是ScheduledFuture对象

2、能够取消定时任务的执行。

通过调用上一步的ScheduledFuturecancel方法,就可以将这个任务取消。

3、动态的修改任务执行的时间。

  1. 先取消任务。
  2. 然后在重新注册一个任务。

4、获取定时任务执行的异常

ThreadPoolTaskScheduler类中有一个设置ErrorHandler的方法,给自己实现的ErrorHandler即可。
定时任务错误处理

提示:

  1. Spring中我们通过@Scheduled注解来实现的定时任务,底层也是通过ThreadPoolTaskScheduler来实现的。可以通过ScheduledAnnotationBeanPostProcessor类来查看。

  2. ThreadPoolTaskScheduler的默认线程数是1,这个需要根据实际的情况进行修改。

三、代码实现

此处只给出动态注册定时任务和取消的定时任务的代码。

package com.huan.study.task.jobs.tasks;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronExpression;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author huan.fu 2021/7/8 - 下午2:46
 */
@Component
@Slf4j
public class DynamicCronTask implements InitializingBean {

    @Autowired
    private ThreadPoolTaskScheduler taskScheduler;


    private ScheduledFuture<?> scheduledFuture;

    @Override
    public void afterPropertiesSet() throws Exception {
        // 动态启动一个定时任务
        log.info("注册一个定时任务:每隔1秒执行一次");
        scheduledFuture = register("* * * * * ?");

        // 取消一个调度
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(5);
                log.info("取消调度");
                scheduledFuture.cancel(false);
                log.info("取消结果:" + scheduledFuture.isCancelled());
                log.info("重新注册一个定时任务:每隔2秒执行一次");
                register("*/2 * * * * ?");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    private ScheduledFuture<?> register(String cron) {

        // 高版本使用 CronExpression,低版本使用 CronSequenceGenerator
        boolean validExpression = CronExpression.isValidExpression(cron);
        log.info("cron:[{}]是合法的吗:[{}]", cron, validExpression);

        CronExpression expression = CronExpression.parse(cron);
        LocalDateTime nextExecTime = expression.next(LocalDateTime.now());
        if (null != nextExecTime) {
            log.info("定时任务下次执行的时间为:[{}]", nextExecTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }

        return taskScheduler.schedule(new Runnable() {
            @Override
            public void run() {
                log.info("我执行了");
            }
        }, new CronTrigger(cron));
    }
}

四、执行结果

执行结果

五、完整代码

https://gitee.com/huan1993/spring-cloud-parent/tree/master/springboot/springboot-task

  • 9
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值