Home > Java > javaTutorial > Implementation code for SpringBoot dynamic management of scheduled tasks

Implementation code for SpringBoot dynamic management of scheduled tasks

不言
Release: 2018-09-11 14:20:58
Original
4345 people have browsed it

The content of this article is about the implementation code of SpringBoot dynamic management of scheduled tasks. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

SpringBoot dynamically manages scheduled tasks

First use @EnableScheduling to open scheduled tasks

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);
    }
}
Copy after login

Configure log level

logging.level.com= debug
logging.file=springboot-scheduled.log
Copy after login

Fixed period scheduled tasks

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...");
    }


}
Copy after login

Dynamically modify the timing cycle

Implement the SchedulingConfigurer interface and override the configureTasks method

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();
    }

}
Copy after login

Observe the printing results, the cycle has changed, but the project has not restarted.

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...
Copy after login

Related recommendations:

SpringBoot scheduling tasks and common task expressions

Manage scheduled tasks through reidis

The above is the detailed content of Implementation code for SpringBoot dynamic management of scheduled tasks. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template