0

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. 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). My code not using the readed db value at the moment, but copied my code below.


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

}
csirkeautomata
  • 177
  • 3
  • 16
  • question is not clear `i would like to access it into my @Scheduled(fixedDelay = period). My code not using the readed db value at the moment` – Ryuzaki L Jan 26 '19 at 16:10
  • If my @Scheduled task is runnin I would like to read data from the db and update my periods value. Now I am not able to use variables for fixedDelay because it must be a constant. – csirkeautomata Jan 26 '19 at 16:13
  • Possible duplicate of [How to change Spring's @Scheduled fixedDelay at runtime](https://stackoverflow.com/questions/15250928/how-to-change-springs-scheduled-fixeddelay-at-runtime) – sbstnzmr Jan 26 '19 at 16:18

2 Answers2

0

You can create a new scheduler inside your @Scheduled method.

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

delay is this you've read from DB.

0

What is the question? Does the scheduling not work? Did you @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).

sbstnzmr
  • 401
  • 7
  • 21
  • Actually I am not able to change fixedDelay because the value of fixedDelay must be constant. I have just one @Scheduled task..and I want that task to read the data for itself..so if the data has been readed from database I would like to update my period variable's value. – csirkeautomata Jan 26 '19 at 16:11
  • So you want to do [this](https://stackoverflow.com/questions/14630539/scheduling-a-job-with-spring-programmatically-with-fixedrate-set-dynamically/14632758#14632758)? – sbstnzmr Jan 26 '19 at 16:18