简体   繁体   English

Spring Boot @Scheduled fixedDelay操作

[英]Spring Boot @Scheduled fixedDelay manipulation

I am building an application what reads data from the database in every 'n' seconds.. in my @Scheduled method i would like to read data from my database, for configuring my @Scheduled task. 我正在构建一个应用程序,该应用程序每隔'n'秒就会从数据库中读取一次数据..在我的@Scheduled方法中,我想从数据库中读取数据以配置@Scheduled任务。 It is important to read in every period, because it can change anytime. 在每个时期阅读都是很重要的,因为它可以随时更改。 So when i readed the data 'period' from my db table, i would like to access it into my @Scheduled(fixedDelay = period). 因此,当我从数据库表中读取数据“期间”时,我想将其访问到我的@Scheduled(fixedDelay = period)中。 My code not using the readed db value at the moment, but copied my code below. 我的代码目前不使用读取的db值,而是在下面复制了我的代码。


Code: 码:

@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..

}

You can create a new scheduler inside your @Scheduled method. 您可以在@Scheduled方法中创建一个新的调度程序。

ScheduledExecutorService executor = Executors.newScheduledThreadPool(4);
executor.scheduleWithFixedDelay(() => {
    // do your stuff
}, 0, delay, TimeUnit.SECONDS);

delay is this you've read from DB. delay是您从数据库读取的。

What is the question? 有什么问题 Does the scheduling not work? 调度不起作用吗? Did you @EnableScheduling ? @EnableScheduling吗? Does it work but not with your variable? 它可以正常工作,但不能与您的变量一起使用吗?

If I understand correctly you would read a variable from the db and then you want to schedule something in the delay of that variable. 如果我理解正确,则可以从数据库中读取一个变量,然后在该变量的延迟时间内进行调度。 So you would need to read the data from the database first, set it to a variable delay and then call another method with @Scheduled(fixedDelay = delay) . 因此,您需要首先从数据库读取数据,将其设置为可变的delay ,然后使用@Scheduled(fixedDelay = delay)调用另一个方法。

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

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