55

I know I can inject the value from properties file with the following syntax:

@Scheduled(fixedRate=${myRate})
public void getSchedule(){
    System.out.println("in scheduled job");
}

However I can't guess how to accomplish the same if the configuration is in YAML file.

Thanks in advance,

Juan Carlos González
  • 1,024
  • 2
  • 9
  • 22

3 Answers3

111

In my application.properties (YAML) I put this

console:
    fetchMetrics: 5000

Then in my simple Task class I push the definition :

@Scheduled(fixedRateString ="${console.fetchMetrics}", initialDelay=1000)
public void fetchMetrics() {
    logger.info("What's up ?");
}

Please notice that fixedRate expects a long and you want to inject a placeholder, you will need fixedRateString

rajadilipkolli
  • 3,475
  • 2
  • 26
  • 49
Wajax
  • 1,220
  • 1
  • 11
  • 8
  • 1
    Thanks for your answer. When I try this on a Spring Boot project, I'm getting an IllegalStateException with the message Invalid fixedRateString value "${general.pathMonitorPollingRate}" - cannot parse into integer. I've debugged this error into AbstractBeanFactory from ScheduledAnnotationBeanPostProcessor and I've seen that the value cannot be resolved because the resolvers list is empty. Don't know if this is configuration problem or an issue in Boot. – Juan Carlos González Jan 08 '15 at 12:10
  • @JuanCarlosGonzález This is probably won't help you anymore, but I was running into the same "cannot parse into integer" issue. I am able to get around it by adding the Configuration, EnableAutoConfiguration and EnableScheduling annotations to my config. Without auto configuration, it breaks. Can't figure out why – k.schroeder31 Feb 05 '16 at 19:52
  • Thanks for your help. I think @EnableAutoConfiguration must register the proper StringValueResolver in order to evaluate annotation properly. – Juan Carlos González Feb 08 '16 at 08:28
  • 2
    The "ConsoleConfig" class isn't required here - it doesn't add any value and isn't used at all since the configuration value is taken directly from the YAML and used for the fixedRateString – Tom Feb 20 '19 at 13:06
  • 1
    Note that your property can also be string resolved by Duraction.parse() ie: "PT20.345S" for 20.345 seconds, "P2D" for two days and so on. It doesn't have to be only long with given milliseconds. https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html#fixedRateString-- – estn Jan 22 '20 at 12:09
24

I find it easy once done for my project.
Change fixedRate to fixedRateString and put the property key in double quotes like this:

@Scheduled(fixedRateString="${myRate}")
public void getSchedule() {
    System.out.println("Scheduled job");
}
Pavel Gordon
  • 192
  • 2
  • 11
abhishek ringsia
  • 1,970
  • 2
  • 20
  • 28
0

In my application I use the annotation PropertySource on my config class:

@PropertySource("application-${spring.profiles.active}.yml")

spring.profiles.active returns the active profile (dev, test, etc). My properties file name is application-dev.yml

The annotation @Scheduled works with property injection. Dont forget the annotation with prefix configuration on your class.

Fred Barclay
  • 834
  • 1
  • 13
  • 24