首页 > Java > java教程 > SpringBoot动态管理定时任务的实现代码

SpringBoot动态管理定时任务的实现代码

不言
发布: 2018-09-11 14:20:58
原创
4322 人浏览过

本篇文章给大家带来的内容是关于SpringBoot动态管理定时任务的实现代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

SpringBoot动态管理定时任务

首先使用@EnableScheduling 开启定时任务

package com.fighting;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class ScheduledApplication {

    public static void main(String[] args) {
        SpringApplication.run(ScheduledApplication.class, args);
    }
}
登录后复制

配置日志级别

logging.level.com= debug
logging.file=springboot-scheduled.log
登录后复制

固定周期定时任务

package com.fighting;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Spring静态周期定时任务
 *
 * @author fighting
 * @date 2018-09-11
 */
@Component
public class SpringStaticCronTask {

    public static final Logger logger = LoggerFactory.getLogger(SpringStaticCronTask.class);


    @Scheduled(cron = "0/5 * * * * ?")
    public void staticCornTask() {
        logger.debug("staticCronTask is running...");
    }


}
登录后复制

动态修改定时周期

实现SchedulingConfigurer 接口,重写 configureTasks方法

package com.fighting;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

/**
 * 动态定时任务
 *
 * @author fighting
 * @date 2018-09-11
 */
@Component
public class SpringDynamicCronTask implements SchedulingConfigurer {

    private static final Logger logger = LoggerFactory.getLogger(SpringDynamicCronTask.class);

    private static String cron = "0/5 * * * * ?";


    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(() -> {
            // 任务逻辑
            logger.error("dynamicCronTask is running...");
        }, triggerContext -> {
            // 任务触发,在这里可修改任务的执行周期,因为每次调度都会执行这里
            CronTrigger cronTrigger = new CronTrigger(cron);
            return cronTrigger.nextExecutionTime(triggerContext);
        });

    }


    public SpringDynamicCronTask() {
        //模拟业务修改周期,可以在具体业务中修改参数cron
        new Thread(() -> {
            try {
                Thread.sleep(15000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            cron = "0/2 * * * * ?";
        }).start();
    }

}
登录后复制

观察打印结果,周期变了,但是项目没有重启。

2018-09-11 13:36:50.009 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask        : staticCronTask is running...
2018-09-11 13:36:50.010 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:36:55.001 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask        : staticCronTask is running...
2018-09-11 13:36:55.002 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:00.009 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask        : staticCronTask is running...
2018-09-11 13:37:00.016 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:05.016 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask        : staticCronTask is running...
2018-09-11 13:37:05.016 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:06.013 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:08.008 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:10.002 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:10.003 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask        : staticCronTask is running...
2018-09-11 13:37:12.002 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:14.006 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:15.015 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask        : staticCronTask is running...
2018-09-11 13:37:16.012 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:18.002 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
登录后复制

相关推荐:

SpringBoot 调度任务及常用任务表达式

通过reidis管理定时任务

以上是SpringBoot动态管理定时任务的实现代码的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板