本篇内容主要讲解“SpringBoot项目中怎么使用@Scheduled读取动态参数”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“springBoot项目中怎么使用@Scheduled读取动态参
本篇内容主要讲解“SpringBoot项目中怎么使用@Scheduled读取动态参数”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“springBoot项目中怎么使用@Scheduled读取动态参数”吧!
application.propertites: read.timer.parmas=0 0/1 * * * *
定时类:
@Componentpublic class ScheduledService {Logger logger= LoggerFactory.getLogger(ScheduledService.class); @Scheduled(cron = "${read.timer.parmas}") public void readConfigTable(){ logger.info("*****.read.timer.parmas"); }}
启动类:
@SpringBootApplication@EnableScheduling //必须public class DataApplication { public static void main(String[] args) { SpringApplication.run(DataApplication.class,args); }}
(1)核心代码
@Component@EnableSchedulingpublic 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)其他类或方法动态传参赋值
TestScheduledParams.corn="0/20 * * * * *"
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 {//实现SchedulingConfigurer 接口 @Autowired CommonUtil commonUtil; @Autowired TaskService taskService; private static String cron; @Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {//重写该方法 scheduledTaskRegistrar.addTriggerTask(doTask(), getTrigger()); } private Runnable doTask() { return new Runnable() { @Override public void run() { log.info("-------------------------------执行"+cron); taskService.monDay();//真正需要运行的逻辑代码 } }; } private Trigger getTrigger() { return new Trigger() { @Override public Date nextExecutionTime(TriggerContext triggerContext) { // 触发器 CronTrigger trigger = new CronTrigger(getCron()); return trigger.nextExecutionTime(triggerContext); } }; } public String getCron() { Jedis jedis = commonUtil.getJedis(); String newCron = jedis.get("cron");//可以改成配置到数据库中 if (StringUtils.isEmpty(newCron)) { jedis.set("cron","0 30 0 ? * Mon"); return "0 30 0 ? * Mon"; } if (!newCron.equals(cron)) { log.info(new StringBuffer("Cron has been changed to:'").append(newCron).append("'. Old cron was:'").append(cron).append("'").toString()); cron = newCron; } return cron; }}
到此,相信大家对“SpringBoot项目中怎么使用@Scheduled读取动态参数”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
--结束END--
本文标题: SpringBoot项目中怎么使用@Scheduled读取动态参数
本文链接: https://lsjlt.com/news/304499.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0