SpringBoot Dynamically Modifies Scheduled
Scenario:
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
For example:
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时间格式字符串")); }
SpringBoot project @Scheduled to read dynamic parameters
1. Based on @Scheduled configurable development
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); } }
2. Based on code implementation
(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!

Using POI library in Java to add borders to Excel files Many Java developers are using Apache...

Efficient processing of batch interface requests: Using CompletableFuture to ensure that concurrent calls to third-party interfaces can significantly improve efficiency when processing large amounts of data. �...

In JavaWeb applications, the feasibility of implementing entity-class caching in Dao layer When developing JavaWeb applications, performance optimization has always been the focus of developers. Either...

The current status of motorcycle and motorcycle systems and ecological development of motorcycle systems, as an important bridge connecting knights and vehicles, has developed rapidly in recent years. Many car friends...

When using MyBatis-Plus or tk.mybatis...

How to query personnel data through natural language processing? In modern data processing, how to efficiently query personnel data is a common and important requirement. ...

In processing next-auth generated JWT...

In IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)