Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify Scheduled dynamically by SpringBoot

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to modify Scheduled dynamically by SpringBoot". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "SpringBoot how to dynamically modify Scheduled" bar!

SpringBoot dynamically modifies the Scheduled scene:

Configurable Scheduled execution time. Normal Scheduled is fixed when the project starts. There is no way to automatically update the Scheduled execution time according to the calling background code.

For example:

The system starts the read time Cron: 0.03 * *?. By executing the background method, you can dynamically configure the Cron time format, clear the original task execution, and execute the new timed task time.

1. Dynamically modify scheduled tasks according to ThreadPoolTaskScheduler and ScheduledFuture classes (ThreadPoolTaskScheduler this class cannot use @ Autowired, 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 put it in the run thread method)

ThreadPoolTaskScheduler = new ThreadPoolTaskScheduler (); threadPoolTaskScheduler.initialize (); if (futureworthy null) {future.cancel (true);} future=threadPoolTaskScheduler.schedule (object,new CronTrigger ("Cron time format string required"))

The above logic-can realize dynamic Scheduled configuration.

The following logic-configure the project to start automatic reading DB Cron setting timing

1. @ Order and implement the CommandLineRunner class override method run

@ Override public void run (String... Args) throws Exception {logger.info ("system startup default setting reconciliation task time"); / / get the reconciliation time GetBillTimeResp time = systemConfigService.getTime () currently set by DB; / / get the Cron time format string String timeCron = billTimeCronFormat (time.getBillTime ()); logger.info ("time is:" + timeCron); / / Scheduler setting is executed every day. ThreadPoolTaskScheduler = new ThreadPoolTaskScheduler (); threadPoolTaskScheduler.initialize (); future=threadPoolTaskScheduler.schedule (object,new CronTrigger ("DB Cron time format string"));} SpringBoot project @ Scheduled reads dynamic parameters 1, based on @ Scheduled configurable development application.propertites: read.timer.parmas=0 0max 1 *

Timing class:

@ Componentpublic 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, code-based implementation

(1) Core code

@ Component@EnableSchedulingpublic class TestScheduledParams implements SchedulingConfigurer {Logger logger= LoggerFactory.getLogger (TestScheduledParams.class); public static String DEFAULT_CORN= "0ram 3 *"; / / # # dynamic parameters to be passed to the default value. Public static String corn=DEFAULT_CORN; @ Override public void configureTasks (ScheduledTaskRegistrar taskRegistrar) {taskRegistrar.addTriggerTask (new Runnable () {@ Override public void run () {/ / logger.info (scheduled task logic)) }}, new Trigger () {@ Override public Date nextExecutionTime (TriggerContext triggerContext) {/ / task trigger, you can modify the task execution cycle CronTrigger cronTrigger = new CronTrigger (corn); Date date = cronTrigger.nextExecutionTime (triggerContext); return date;}});}}

(2) dynamic parameter assignment of other classes or methods

TestScheduledParams.corn= "0tap 20 *" so far, I believe you have a deeper understanding of "how to dynamically modify Scheduled by SpringBoot". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report