1

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?

waldrabe
  • 303
  • 2
  • 13
  • An alternative way could be to use scheduling-tasks, there is an example available here: https://spring.io/guides/gs/scheduling-tasks/ – Greg Sep 22 '21 at 09:40
  • 1
    @Greg I know this example. It uses the @Scheduled(fixedRate = 5000) annotation which I mentioned in my question. It uses a compile time constant, but I need a value that can change at runtime. – waldrabe Sep 22 '21 at 09:53

0 Answers0