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...

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,可以使用Quartz库来实现动态移除定时任务。Quartz是一个强大的调度框架,可以在后台添加、修改、移除定时任务。通过集成Quartz,可以实现在不重启应用的情况下动态移除定时任务。 可以参考中的Demo来实现动态移除定时任务的功能。在这个Demo中,后台可以添加、修改、移除定时任务,并且可以查看当前任务的状态。具体的实现可以参考Git项目地址中提供的代码。 使用Spring Boot和Quartz实现动态移除定时任务的步骤如下: 1. 首先,需要在Spring Boot项目中添加Quartz的依赖。 2. 创建一个定时任务管理类,用于管理所有的定时任务。这个类可以包含添加、修改、移除定时任务的方法。 3. 在定时任务管理类中,定义一个方法来移除定时任务。这个方法可以接受一个任务的唯一标识符作为参数,然后使用Quartz提供的API来移除该任务。 4. 在需要移除定时任务的地方,调用定时任务管理类中的移除定时任务的方法,传入要移除的任务的标识符即可。 通过以上步骤,就可以实现在Spring Boot中动态移除定时任务的功能了。这样可以在不重启应用的情况下,根据需要随时移除指定的定时任务。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Springboot2-Quartz 后台可动态配置的定时任务](https://download.csdn.net/download/qq_32923745/10981179)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Spring Boot实现动态增加、删除、修改、停止定时任务](https://blog.csdn.net/qq_43813937/article/details/104183541)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [springboot不重启应用动态添加修改删除定时任务(以cron定时方式为例)](https://blog.csdn.net/qq_37549757/article/details/94393282)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值