Configurable Scheduled execution time, normal Scheduled is fixed when the project starts, there is no way to automatically update Scheduled based on calling the background code Execution time
System startup reading time Cron: 0 0 3 * * ?, the Cron time format can be dynamically configured by executing the background method, and the original execution task can be cleared and the new execution time can be executed Setting the scheduled task time
1. Dynamically modify the scheduled task according to the ThreadPoolTaskScheduler and ScheduledFuture classes (the ThreadPoolTaskScheduler class cannot use @Autowired and directly define member variables)
private ThreadPoolTaskScheduler threadPoolTaskScheduler; private ScheduledFuture<?> future;
2. Dynamically modify the Scheduled background method Logic (object is the timing logic that the Runnable implementation class needs to execute, and is placed in the run thread method)
threadPoolTaskScheduler = new ThreadPoolTaskScheduler(); threadPoolTaskScheduler.initialize(); if(future!=null){ future.cancel(true); } future=threadPoolTaskScheduler.schedule(object,new CronTrigger("需要Cron时间格式字符串") );
The above logic ------------------ -----You can realize dynamic Scheduled configuration
The following logic-----------------------Configuration items Start automatic reading of DB Cron, set timing
1, @Order and implement the CommandLineRunner class override method run
@Override public void run(String... args) throws Exception { logger.info("系统启动 默认设置对账任务 时间"); //获取目前DB 设置的对账时间 GetBillTimeResp time = systemConfigService.getTime(); //获取Cron时间格式字符串 String timeCron = billTimeCronFormat(time.getBillTime()); logger.info("时间为:"+timeCron); //Scheduler 设置每天执行。。。 threadPoolTaskScheduler = new ThreadPoolTaskScheduler(); threadPoolTaskScheduler.initialize(); future=threadPoolTaskScheduler.schedule(object,new CronTrigger("DB Cron时间格式字符串")); }
application.propertites: read.timer.parmas=0 0/1 * * * *
Timing class:
@Component public class ScheduledService { Logger logger= LoggerFactory.getLogger(ScheduledService.class); @Scheduled(cron = "${read.timer.parmas}") public void readConfigTable(){ logger.info("*****.read.timer.parmas"); } }
Startup class:
@SpringBootApplication @EnableScheduling //必须 public class DataApplication { public static void main(String[] args) { SpringApplication.run(DataApplication.class,args); } }
(1) Core Code
@Component @EnableScheduling public class TestScheduledParams implements SchedulingConfigurer{ Logger logger= LoggerFactory.getLogger(TestScheduledParams.class); public static String DEFAULT_CORN="0/3 * * * * *"; //##动态传参要给默认值。 public static String corn=DEFAULT_CORN; @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.addTriggerTask(new Runnable() { @Override public void run() { // logger.info("定时任务逻辑"); } }, new Trigger() { @Override public Date nextExecutionTime(TriggerContext triggerContext) { //任务触发,可修改任务的执行周期 CronTrigger cronTrigger = new CronTrigger(corn); Date date = cronTrigger.nextExecutionTime(triggerContext); return date; } }); } }
(2) Dynamic parameter assignment and assignment of other classes or methods
TestScheduledParams.corn="0/20 * * * * *"
The above is the detailed content of How to dynamically modify Scheduled in SpringBoot. For more information, please follow other related articles on the PHP Chinese website!