简体   繁体   中英

Dynamic configuration for @Scheduled in Spring Boot

I'm trying to dynamically configure a Spring Boot schedule using the configuration file.

The goal is to have the following in my application.yml :

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

What I'm struggling with, is how I can now apply this configuration to the @Scheduled annotations. I was thinking something like the following:

Scheduler.java:

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

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

Use full path of property in your configuration.

Corn expression should be 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() {
    // 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM