简体   繁体   中英

@scheduled annotation with variable fixeddelay

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. Eg If I want to be able to change the delay time/frequency of method execution at runtime without stopping the application ie 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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM