SpringBoot项目@Scheduled读取动态参数

1、基于@Scheduled可配置开发

application.propertites:

read.timer.parmas=0 0/1 * * * *
定时类:
@Component
public 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);
    }
}

2、基于代码实现

(1)核心代码
@Component
@EnableScheduling
public 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 * * * * *"

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Spring Boot中,@Scheduled注解用于创建定时任务。要基于Consul动态修改定时任务,你需要使用Consul的配置中心功能来动态修改定时任务的执行时间。 首先,你需要在Spring Boot项目中添加Consul的依赖。可以在项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-config</artifactId> </dependency> ``` 然后,在Spring Boot的配置文件中配置Consul的相关信息,包括Consul的地址和端口等。可以在application.properties或application.yml文件中添加以下配置: ```yaml spring.cloud.consul.host=your-consul-host spring.cloud.consul.port=your-consul-port ``` 接下来,你可以在定时任务的方法上添加@Scheduled注解,并使用SpEL表达式来指定定时任务的执行时间。例如,你可以使用以下方式来动态修改定时任务的执行时间: ```java @Scheduled(cron = "${scheduled.expression}") public void scheduledTask() { // 定时任务的逻辑代码 } ``` 在Consul的配置中心中,你可以设置一个名为"scheduled.expression"的配置项,用于指定定时任务的执行时间。当你在Consul的配置中心修改了该配置项的值时,定时任务的执行时间也会相应地被修改。 总结起来,要基于Consul动态修改Spring Boot中的@Scheduled定时任务,你需要添加Consul的依赖,配置Consul的相关信息,并在定时任务的方法上使用SpEL表达式来指定定时任务的执行时间。然后,通过修改Consul的配置中心中的配置项的值,即可动态修改定时任务的执行时间。 #### 引用[.reference_title] - *1* *3* [使用 SpringBoot Admin 监控你的 SpringBoot 程序](https://blog.csdn.net/weixin_38405253/article/details/103760018)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [SpringBoot](https://blog.csdn.net/qq_43701330/article/details/109603305)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郝少

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值