user12387650
user12387650

Reputation: 31

Spring Boot @Scheduled with Different Profiles

I have a class in a Spring Boot application (Spring Boot ver. 1.5.10) that uses the @Scheduled annotation on a method and I want to manage the value of the execution interval for this scheduler in my application.yaml file. I was able to find answers on how to do this using fixedRateString = "${some.interval.value}". The problem is that I have multiple profiles in the same application.yaml file and Spring is always selecting the value in the default profile. This happens even when I specify a different active profile (I can see that the correct non-default profile is active in the startup logs).

Is there a way to get Spring to use the value from the active profile instead of the default profile? I've seen solutions that utilize multiple application.yaml files (e.g. application-dev.yaml, application-prod.yaml). But this seems really clunky, and I'd like to avoid this if at all possible because we have some properties that don't change between profiles and I don't want to duplicate the values for maintenance reasons. Below is an example of my setup.

application.yaml

scheduled:
  rateValue = 1000

---

spring:
  profile: prod

scheduled:
  rateValue = 2000

Service Class

public class foo {

    @Scheduled(fixedRateString = "${scheduled.rateValue}")
    public void bar() {
        // Do stuff
    }
}

Upvotes: 0

Views: 749

Answers (1)

Ervin Szilagyi
Ervin Szilagyi

Reputation: 16775

We can create multi-profile application.yml files:

scheduled:
    rateValue = 1000
---
spring:
    profiles: development
scheduled:
    rateValue = 1500
---
spring:
    profiles: production
scheduled:
    rateValue = 2000

Values which don't change can be omitted from given profiles.

More information: Spring Boot documentation

Upvotes: 4

Related Questions