Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @Scheduled inject delay time

I have a few methods annotated with

@Scheduled(fixedDelay = 6000)
private void myScheduledMethod(){
//do something
}

I also have a set of properties files where I configure environment specific values. For testing purposes I'd like the value of the delay to be configurable, ideally through a property in a properties file.

Since the value of fixedDelay has to be a constant, I'm looking for a way to get this set from a properties file, but haven't found a way to do it yet.

like image 532
Iker Jimenez Avatar asked Feb 03 '23 00:02

Iker Jimenez


2 Answers

I got stuck on the same issues but the best way to solve this now would be:

@Scheduled(fixedDelayString = "${my.delay.property}")
public void myScheduledMethod(){
    // do something
}
like image 78
user3116058 Avatar answered Feb 05 '23 15:02

user3116058


It would be good to have this option, but I think it does not exist (the annotation is class-level, while the value would be injected when an instance is created).

In order to make this configurable use the xml namespace <task:. Like the example from the spring docs:

<task:scheduled-tasks scheduler="myScheduler">
    <task:scheduled ref="someObject" method="someMethod" 
         fixed-delay="${configuredDelay}"/>
</task:scheduled-tasks>
like image 22
Bozho Avatar answered Feb 05 '23 16:02

Bozho