I am using the @Scheduled
annotation from Spring framework to invoke a method. But I have multiple nodes in my setup and I do not want them to all run at exactly the same time. So I'd like to set a random value to the initial delay to offset them from each other.
import org.springframework.scheduling.annotation.Scheduled;
@Scheduled(fixedRate = 600000, initialDelay = <random number between 0 and 10 minutes> )
Unfortunately, I am only allowed to use a constant expression here. Is there any other way around this? I thought of using Spring expression language.
To make the initial delay randomly somewhere between 0 and the fixedRate
try this:
@Scheduled(fixedDelayString = "${some.delay}", initialDelayString = "${random.int(${some.delay})}")
Where you define some.delay
(but pick a more suitable name) as 10 minutes as a property like so in your application.properties or equivalent.
some.delay = 600000
Of course if you want to be lazy and hard code it you can always just use ${random.int(600000)}