SpringBoot How to dynamically modify Scheduled of system startup default execution dynamically modify
- 2021-10-24 22:52:30
- OfStack
SpringBoot dynamically modifies Scheduled
Scenario:
Configurable Scheduled execution time, normal Scheduled is fixed when the project is started, there is no way to automatically update Scheduled execution time according to the calling background code
For example:
System startup read 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 clearly removed, and the new setting timing task time can be executed
1. According to ThreadPoolTaskScheduler, ScheduledFuture classes dynamically modify timing tasks (ThreadPoolTaskScheduler can't use @ Autowired, and directly define member variables)
private ThreadPoolTaskScheduler threadPoolTaskScheduler;
private ScheduledFuture<?> future;
2. Dynamically modify the background method logic of Scheduled (object is the timing logic that Runnable implementation class needs to execute and put it into run thread method)
threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.initialize();
if(future!=null){
future.cancel(true);
}
future=threadPoolTaskScheduler.schedule(object,new CronTrigger(" Need Cron Time format string ") );
The above logic----------------------------------------------------------------
The following logic----------------------------------------------------------------------------
1. @ Order and implement the CommandLineRunner class override method run
@Override
public void run(String... args) throws Exception {
logger.info(" System startup Setting reconciliation tasks by default Time ");
// Gets the current DB Setting reconciliation time
GetBillTimeResp time = systemConfigService.getTime();
// Get Cron Time format string
String timeCron = billTimeCronFormat(time.getBillTime());
logger.info(" The time is: "+timeCron);
//Scheduler Set daily execution. . .
threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.initialize();
future=threadPoolTaskScheduler.schedule(object,new CronTrigger("DB Cron Time format string ")); }
SpringBoot Project @ Scheduled Read Dynamic Parameters
1. Configurable development based on @ Scheduled
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 // Must
public class DataApplication {
public static void main(String[] args) {
SpringApplication.run(DataApplication.class,args);
}
}
2. Implementation based on code
(1) Core code
@Component
@EnableScheduling
public class TestScheduledParams implements SchedulingConfigurer{
Logger logger= LoggerFactory.getLogger(TestScheduledParams.class);
public static String DEFAULT_CORN="0/3 * * * * *";
//## Dynamic parameters should be passed to default values.
public static String corn=DEFAULT_CORN;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
// logger.info(" Timed task logic ");
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
// Task triggering, which can modify the execution cycle of a task
CronTrigger cronTrigger = new CronTrigger(corn);
Date date = cronTrigger.nextExecutionTime(triggerContext);
return date;
}
});
}
}
(2) Dynamic parameter transfer and assignment of other classes or methods
TestScheduledParams.corn="0/20 * * * * *"