[英]Spring Boot @Scheduled fixedDelay manipulation
我正在构建一个应用程序,该应用程序每隔'n'秒就会从数据库中读取一次数据..在我的@Scheduled方法中,我想从数据库中读取数据以配置@Scheduled任务。 在每个时期阅读都是很重要的,因为它可以随时更改。 因此,当我从数据库表中读取数据“期间”时,我想将其访问到我的@Scheduled(fixedDelay = period)中。 我的代码目前不使用读取的db值,而是在下面复制了我的代码。
码:
@Scheduled(fixedDelay = 60000)
public void startSchedule() throws InterruptedException {
//read data from db to configure Scheduling
//equalize fixedDelay = db.getPeriod(); -> i am not able to do that...
//do other fancy thing..
}
您可以在@Scheduled方法中创建一个新的调度程序。
ScheduledExecutorService executor = Executors.newScheduledThreadPool(4);
executor.scheduleWithFixedDelay(() => {
// do your stuff
}, 0, delay, TimeUnit.SECONDS);
delay
是您从数据库读取的。
有什么问题 调度不起作用吗? 您@EnableScheduling
吗? 它可以正常工作,但不能与您的变量一起使用吗?
如果我理解正确,则可以从数据库中读取一个变量,然后在该变量的延迟时间内进行调度。 因此,您需要首先从数据库读取数据,将其设置为可变的delay
,然后使用@Scheduled(fixedDelay = delay)
调用另一个方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.