I have a spring boot application that runs indefinitely as a service, cycling through its tasks every x seconds. This cycle time comes from a configuration file and can change at runtime. I used a self-made infinite while loop for that:
@Component
public class MainCycle implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
while (true) {
...
waitCycleTime()
}
}
}
I have read that you shouldn't implement your own infinite loops when using Spring Boot (Spring Boot - infinite loop service), but if I used Spring's @Scheduled(fixedRate=500)
annotation, I'd have to use a compile time constant for the cycle time.
What is the best way to implement an infinite loop with a cycle time that can change during runtime when using Spring Boot?