Spring @Scheduled annotation random delay

In this working example, the random delay will be between 5 and 10 seconds.

@Scheduled(fixedDelayString = "#{new Double((T(java.lang.Math).random() + 1) * 5000).intValue()}")

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.


Keep in mind, that the initialDelayString is evaluated only once at startup and then this same values is used whenever the job is scheduled.

See org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor#processScheduled


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)}