【问题标题】:Dynamic configuration for @Scheduled in Spring BootSpring Boot 中 @Scheduled 的动态配置
【发布时间】:2018-12-23 11:18:08
【问题描述】:

我正在尝试使用配置文件动态配置 Spring Boot 计划。

目标是在我的application.yml 中包含以下内容:

platform:
  plata:
    schedule:
      cron: '0 0 9 * * *'
  platb:
    schedule:
      initialDelay = 20000
      fixedDelay = 10000000

我正在苦苦挣扎的是,我现在如何将此配置应用于 @Scheduled 注释。我在想类似以下的事情:

Scheduler.java:

@Scheduled("${platform.plata.schedule}")
public void plata() throws CalculationException {
    ...
}

@Scheduled("${platform.platb.schedule}")
public void platb() throws CalculationException {
    ...
}

【问题讨论】:

  • 您可能会在以编程方式配置调度程序方面做得更好:baeldung.com/spring-task-scheduler这对您来说是一个选择吗?
  • @Simon Martinelli:感谢您的建议,但不幸的是,这不是我的选择。

标签: java spring-boot yaml spring-scheduled


【解决方案1】:

在配置中使用属性的完整路径。

Corn 表达式应该是0 0 9 * * * note no '-chars

@Scheduled(cron="${platform.plata.schedule.cron}")
public void withCron() {
    // 
}

@Scheduled(initialDelayString = "${platform.platb.schedule.initialDelay}" ,fixedDelayString= "${platform.platb.schedule.fixedDelay}")
public void withFixedDelay() {
    // 
}

【讨论】:

  • 感谢您的回答!很抱歉,我的问题并不清楚,但我正在寻找的是我可以在配置文件中从 cron 更改为 initialDelayfixedDelay 而无需更改代码。这就是为什么我希望将类似 Map 的东西注入 @Scheduled
猜你喜欢
  • 1970-01-01
  • 2020-10-02
  • 2016-12-22
  • 2019-04-18
  • 1970-01-01
  • 2021-04-30
  • 2019-08-08
  • 1970-01-01
  • 2019-03-10
相关资源
最近更新 更多