In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use spring-task timed tasks to dynamically configure and modify the execution time. I hope you will get something after reading this article. Let's discuss it together.
Spring-task scheduled task dynamic configuration modification execution time
Due to the needs of the project, several scheduled tasks need to dynamically set the execution time, so consult the relevant information, it can be set dynamically, no more nonsense, directly on the code, clear at a glance.
Package com.seckill.quartz;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;import java.text.SimpleDateFormat;import java.util.Date / * * * Created by loup on 2017-11-11. * / @ Component@EnableSchedulingpublic class DynamicScheduledTask implements SchedulingConfigurer {/ / the time expression is executed every 2 seconds private String cron = "0ram 2 *?"; private SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss") @ Override public void configureTasks (ScheduledTaskRegistrar scheduledTaskRegistrar) {scheduledTaskRegistrar.addTriggerTask (new Runnable () {@ Override public void run () {/ / task logic System.out.println ("- start-") System.out.println ("dynamically modify scheduled task parameters, the time expression cron is:" + cron); System.out.println ("current time is:" + sdf.format (new Date (); System.out.println ("- end-") }, new Trigger () {@ Override public Date nextExecutionTime (TriggerContext triggerContext) {CronTrigger cronTrigger = new CronTrigger (cron); Date nextExecDate = cronTrigger.nextExecutionTime (triggerContext); return nextExecDate;}});} public void setCron (String cron) {this.cron = cron;}}
This is a scheduled task scheduling executor, using the way of annotations. First of all, the dynamic configuration should be set to @ EnableScheduling, which is to make sure that it can be dynamic, then implement SchedulingConfigurer and rewrite the configureTasks method, and then there is the relevant spring configuration file for this. To introduce the following task, otherwise you can't recognize it. The configuration file is that simple.
Http://www.springframework.org/schema/task
The next step is to write the test class, whether the test is feasible.
Package com.seckill.quartz;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.io.IOException; / * Created by loup on 2017-11-11. * / @ RunWith (SpringJUnit4ClassRunner.class) @ ContextConfiguration ({"classpath*:/conf/spring-quartz.xml"}) public class QuartzTest {@ Autowired private DynamicScheduledTask dynamicScheduledTask @ Test public void test1 () {try {Thread.sleep (10000);} catch (InterruptedException e) {e.printStackTrace ();} dynamicScheduledTask.setCron ("0max 10 *?"); try {System.in.read ();} catch (IOException e) {e.printStackTrace () }}}
Run the test class, check the results, achieve the effect, and test it yourself.
Spring schedule dynamic configuration execution time
In the past, the time of dynamic modification of scheduled tasks on the saas platform was implemented through a framework such as xx-job, so that we could manage the scheduled tasks of our entire saas platform with a single service, but a small project for the bank recently needed to be localized, so I didn't want to get a lot of services, and they didn't require the modification to take effect immediately. So I directly used spring schedule combined with mysql to dynamically configure the execution time.
The schedule we used before can only use static corn expressions through annotations, SchedulingConfigurer if you want to achieve dynamic, and through the annotation @ EnableScheduling. As follows:
Package com.zqf.marketing.task; import com.zqf.db.marketingrobot.sys.model.RobotSysSwitch;import com.zqf.marketing.sys.service.SwitchService;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;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.Service; import java.util.Date; / * * * @ author zhenghao * @ description * @ date, 2019-1-22 21:50 * / @ Lazy (false) @ Service@EnableSchedulingpublic class TestTaskService implements SchedulingConfigurer {private static Logger log = LoggerFactory.getLogger (TestTaskService.class); @ Autowired private SwitchService switchService; private String SpringDynamicCronTask () {String cron = "0ram 5 *?" / / get the configured corn expression RobotSysSwitch switchById = switchService.getSwitchById (5L); cron = switchById.getSwitchFlag (); log.info (cron); return cron from the database } @ Override public void configureTasks (ScheduledTaskRegistrar scheduledTaskRegistrar) {scheduledTaskRegistrar.addTriggerTask (new Runnable () {@ Override public void run () {/ / task logic log.info ("task_task_tak") }}, new Trigger () {@ Override public Date nextExecutionTime (TriggerContext triggerContext) {String s = SpringDynamicCronTask (); / / Task trigger, which can modify the execution cycle of the task CronTrigger trigger = new CronTrigger (s); Date nextExec = trigger.nextExecutionTime (triggerContext); return nextExec }});}} after reading this article, I believe you have a certain understanding of "how to use spring-task scheduled tasks to dynamically configure and modify the execution time". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.