Question:
I have tasks to do as soon as I upload my application, they are executed repeatedly like this:
@Scheduled(fixedRate = 10000)
public void scheduleFixedRateTask() {
System.out.println("Fixed rate task - " + System.currentTimeMillis()/10000);
}
However sometimes I need to update the time interval and I didn't want to have to stop the application every time to do that. So I was wondering if there is a way to extend @Scheduled from spring so that it reads some configuration file, how?
Answer:
You can parameterize Spring's annotation, the parameter is a little different:
//fixedDelay task:
@Scheduled(fixedDelayString = "${fixedDelay.in.milliseconds}")
//fixedRate task:
@Scheduled(fixedRateString = "${fixedRate.in.milliseconds}")
//A cron expression based task:
@Scheduled(cron = "${cron.expression}")
Then just add the property to your configuration file.
Source: http://www.baeldung.com/spring-scheduled-tasks