as stated in the title of a question, I need to set Spring Scheduler that will run method to load something from database into memory, every day around 4AM
The thing is that I have multiple instances of this server and I don't want all to start executing at the same time cause it will slow down the DB. So I want the time to be at a random minute somewhere between 4:00AM and 4:30AM
So lets say one instance will start everyday at 4:03AM, the other at 4:09AM, third at 4:21AM etc. The execution of query lasts for 1 minute.
Is this possible to do with cron expression, but without using $RANDOM bash (cause I think I dont have it) , or maybe I need to inject this random value some other way into
@Scheduled(cron="* randomMinuteValue 4 * * *")
Using RandomValuePropertySource from Spring
@Scheduled(cron="0 ${random.int[0,30]} 4 * * ?")
In case you have java 8 but not spring boot, you can try the following:
@Scheduled(cron = "0 #{new java.util.Random().nextInt(30)} 4 * * ?")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With