简体   繁体   English

@scheduled注释具有可变fixeddelay

[英]@scheduled annotation with variable fixeddelay

I have a method which is scheduled to run after every 20 minutes. 我有一个计划每20分钟运行一次的方法。 I simply used @Scheduled annotation in Spring boot , However I need a scheduler which takes delay time at runtime. 我只是在Spring boot中使用@Scheduled批注,但是我需要一个调度程序,它需要在运行时延迟时间。 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. 您可以在更改属性文件中的固定速率后使用spring spring上下文或使用spring cloud config来刷新spring上下文,但这会引发一些问题refresh()应该销毁当前生活在上下文中的所有bean(单例等)并重新创建它们,因此任何可能发生的引导都会再次发生。

Here is an reference: Is spring application context reloading via ConfigurableApplicationContext refresh() considered bad-practice 这里是参考: 通过ConfigurableApplicationContext refresh()重新加载Spring应用程序上下文是否被认为是不正确的做法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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