首页 > 编程技术 > java

Spring动态添加定时任务的实现思路

发布时间:2021-7-11 00:35

一、背景

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

二、需求和实现思路

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

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

可以看到返回值是ScheduledFuture对象

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

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

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

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

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

到此这篇关于Spring动态添加定时任务的实现思路的文章就介绍到这了,更多相关Spring定时任务内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

标签:[!--infotagslink--]

您可能感兴趣的文章: