In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use @ Scheduled to read dynamic parameters in SpringBoot project". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use @ Scheduled to read dynamic parameters in SpringBoot projects.
Using @ Scheduled to read dynamic parameter 1, based on @ Scheduled configurable development application.propertites: read.timer.parmas=0 0Unip 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 *" spring boot Scheduled dynamically configure package cn.com.suntree.cmp.service;import cn.com.suntree.cmp.utils.CommonUtil;import lombok.extern.log4j.Log4j2;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.scheduling.Trigger;import org.springframework.scheduling.TriggerContext;import org.springframework.scheduling.annotation.SchedulingConfigurer;import org.springframework.scheduling.config.ScheduledTaskRegistrar;import org.springframework.scheduling.support.CronTrigger Import org.springframework.stereotype.Component;import redis.clients.jedis.Jedis;import java.util.Date;@Component@Log4j2public class MyDynamicTask implements SchedulingConfigurer {/ / implements the SchedulingConfigurer interface @ Autowired CommonUtil commonUtil; @ Autowired TaskService taskService; private static String cron; @ Override public void configureTasks (ScheduledTaskRegistrar scheduledTaskRegistrar) {/ / overrides the method scheduledTaskRegistrar.addTriggerTask (doTask (), getTrigger ()) } private Runnable doTask () {return new Runnable () {@ Override public void run () {log.info ("--execute" + cron); taskService.monDay () / / the logic code that really needs to be run}};} private Trigger getTrigger () {return new Trigger () {@ Override public Date nextExecutionTime (TriggerContext triggerContext) {/ / trigger CronTrigger trigger = new CronTrigger (getCron ()); return trigger.nextExecutionTime (triggerContext) }};} public String getCron () {Jedis jedis = commonUtil.getJedis (); String newCron = jedis.get ("cron"); / / can be configured to if (StringUtils.isEmpty (newCron)) {jedis.set ("cron", "0300? * Mon") in the database; return "0300? * Mon" } if (! newCron.equals (cron)) {log.info (new StringBuffer ("Cron has been changed to:'") .append (newCron) .append ("'. Old cron was:' ") .append (cron) .append ("'") .append ("'"). ToString (); cron = newCron;} return cron;}} so far, I believe you have a deeper understanding of" how to use @ Scheduled to read dynamic parameters in a SpringBoot project ". 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.
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.