Issue
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.
Solution
You can configure the initialDelay through Spring Expression Language:
@Scheduled(fixedRate = 600000, initialDelayString = "#{ T(java.util.concurrent.ThreadLocalRandom).current().nextInt(10*60*1000) }" )
I don't have an IDE to test that code right now, so you may need to adapt that a bit.
Answered By - sk_
Answer Checked By - Clifford M. (JavaFixing Volunteer)