1

I have a method which is scheduled to run after every 20 minutes. I simply used @Scheduled annotation in Spring boot , However I need a scheduler which takes delay time at runtime. E.g. If I want to be able to change the delay time/frequency of method execution at runtime without stopping the application i.e. change the frequency in DB and Code should adapt it.

@Scheduled(initialDelay=15*60*1000, fixedRate=20*60*1000)
public void MyMethod() {
    // Code to repeat after every 20 minutes
    }
}

The fixed rate in the code should be variable and taken at the runtime. Is it possible to achieve it ?

thatman
  • 333
  • 3
  • 14

1 Answers1

0

You can do it with refresh the spring context after change the fixed rate in the properties file, or using spring cloud config, but this will raise some issues - refresh() should destroy all beans currently living in the context (singletons etc) and recreate them, so any bootstrapping that might happen will happen again.

Here is an reference: Is spring application context reloading via ConfigurableApplicationContext refresh() considered bad-practice

Community
  • 1
  • 1
Liping Huang
  • 4,378
  • 4
  • 29
  • 46