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 dynamically modify the Task execution Plan cron by Spring Task

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how Spring Task dynamically modify the task execution plan cron, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Spring Task dynamically modifies the task execution plan cron

Spring Task can dynamically modify the execution time of batch tasks without restarting the service.

Principle

Spring Task only supports modifying the next execution time on TriggerContext (callback SchedulingConfigurer.configureTasks after batch tasks are executed, so that users can reset the Trigger to dynamically modify the next execution time). The disadvantage of this method is that "the execution time cannot take effect in real time".

To this end, take a look at the task document, feel that in order to achieve real-time effective function, you must manually start / stop the Spring task task in the code.

Demo is as follows

First of all, there is no need to configure springTask-related executor and scheduler in xml. Second, customize scheduler and taskRegistrar (classes started by SpringTask) in the code. Finally, a thread is opened to simulate the interface for dynamically modifying cron expressions.

The output is as follows: (35min did not execute the original task)

INFO 27-11 14 34 10 476-Initializing ExecutorService

INFO 27-11 14 34 10 484-nextExecutionTime: 0 35 14 * *?

Modify cron to: 0 3614 * *?

INFO 27-11 14 34 20 487-Initializing ExecutorService

INFO 27-11 14-14-34-20-88-nextExecutionTime: 0 36 14 *?

INFO 27-11 14 3600001-dynamicCronTask is running...

INFO 27-11 14 3600001-nextExecutionTime: 0 36 14 * *?

Spring @ Scheduled scheduled task dynamically modifies cron parameters

Add @ EnableScheduling annotation to the scheduled task class and implement the SchedulingConfigurer interface. (note that the lower version is not valid)

Set a static variable cron, which is used to store the task execution cycle parameters.

Another thread is set up to simulate the external reasons in the actual business to modify the task execution cycle.

Set the task trigger to trigger the task execution, in which you can modify the execution cycle of the task.

Class: SpringDynamicCornTask

Package com.xindatai.ibs.lime.dycSchedul;import java.util.Date;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.context.annotation.Lazy;import org.springframework.scheduling.Trigger;import org.springframework.scheduling.TriggerContext;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.SchedulingConfigurer;import org.springframework.scheduling.config.ScheduledTaskRegistrar;import org.springframework.scheduling.support.CronTrigger;import org.springframework.stereotype.Component / * * Spring dynamic cycle scheduled tasks change the task execution cycle in the case of continuous application * * @ author Liang * * June 1, 2017 * / @ Lazy (false) @ Component@EnableSchedulingpublic class SpringDynamicCornTask implements SchedulingConfigurer {private static final Logger logger = LoggerFactory.getLogger (SpringDynamicCornTask.class); private static String cron; private SpringDynamicCornTask () {cron = "0ram 5 *?" / / start a new thread to simulate externally changed task execution cycle new Thread (new Runnable () {@ Override public void run () {try {Thread.sleep (15 * 1000);} catch (InterruptedException e) {e.printStackTrace ()) } cron = "0ram 10 *?"; System.out.println ("cron change to:" + cron);}}) .start () } @ Override public void configureTasks (ScheduledTaskRegistrar taskRegistrar) {taskRegistrar.addTriggerTask (new Runnable () {@ Override public void run () {/ / task logic logger.info ("dynamicCronTask is running...") }}, new Trigger () {@ Override public Date nextExecutionTime (TriggerContext triggerContext) {/ / task trigger, you can modify the task execution cycle CronTrigger trigger = new CronTrigger (cron); Date nextExecutionTime = trigger.nextExecutionTime (triggerContext); return nextExecutionTime;}});}}

Console:

[INFO 2017-06-01 12:26:25 SpringDynamicCornTask]-dynamicCronTask is running...

[INFO 2017-06-01 12:26:30 SpringDynamicCornTask]-dynamicCronTask is running...

[INFO 2017-06-01 12:26:35 SpringDynamicCornTask]-dynamicCronTask is running...

Cron change to: 0To take 10 *?

[INFO 2017-06-01 12:26:40 SpringDynamicCornTask]-dynamicCronTask is running...

[INFO 2017-06-01 12:26:50 SpringDynamicCornTask]-dynamicCronTask is running...

[INFO 2017-06-01 12:27:00 SpringDynamicCornTask]-dynamicCronTask is running...

The above is all the contents of the article "how Spring Task dynamically modifies the task execution plan cron". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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