23

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.

troymass
  • 1,022
  • 3
  • 11
  • 24
  • Related: [Spring @Scheduled to be started every day at a random minute between 4:00AM and 4:30AM](https://stackoverflow.com/questions/41016433/spring-scheduled-to-be-started-every-day-at-a-random-minute-between-400am-and) – sleske May 12 '23 at 09:58

8 Answers8

20

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.

Eduard Wirch
  • 9,785
  • 9
  • 61
  • 73
sk_
  • 2,105
  • 17
  • 31
  • 1
    Don't SpEl commands need to be in strings? I've only seen them used in string based values. This unfortunately is expecting a long value. I tried it unquoted and got unresolved compilation problems. I tried it quoted as a string and the scheduler never got invoked but no error messages. – troymass Jan 10 '15 at 05:51
  • Have you noticed the use of `initialDelayString` instead of `initialDelay`? The latter takes a long, the first a string which can be an expression. – M. Deinum Jan 10 '15 at 07:34
  • The example does not quite work. First, it must quoted or the compiler will complain. But even then, all I get is null. Is there som kind of configuration that I need to do? – girgen Jun 08 '15 at 15:26
  • Confirmed with multiple Spring versions that this does not work – anupash Aug 01 '18 at 21:19
18

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

mekazu
  • 2,545
  • 3
  • 23
  • 21
  • Just wanted to add this alternative answer as the 'correct' answer isn't really helpful and yet this page is high up in search results. – mekazu Jul 12 '16 at 02:03
  • 1
    Confirmed with multiple Spring versions that this does not work – anupash Aug 01 '18 at 21:19
  • I have used this exact solution in multiple projects, and it worked great. Have not tried it with most recent versions of spring. – Samuel Lindblom Jul 28 '21 at 15:05
5

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()}")
yglodt
  • 13,807
  • 14
  • 91
  • 127
2

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

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • Do you know a way to have the scheduler not only have a constant random value evaluated at startup, but a random value between each triggering? – yglodt Jan 23 '17 at 21:36
1

This should work

@Scheduled(fixedRate = 600000, initialDelay = "#{new java.util.Random().nextInt(700)}")

Verified it with this simple test:

    @Test
    public void testSpEL() {
       ExpressionParser parser = new SpelExpressionParser();
       Expression exp = parser.parseExpression("new java.util.Random().nextInt(500)");
       Integer value =(Integer) exp.getValue();
       Assertions.assertThat(value).isNotNull();
}
slorinc
  • 2,264
  • 1
  • 11
  • 8
0

In kotlin this works:

@Component
class MyJob {
   companion object {
      const val INTERVAL = 24*3600*1000L // once a day
   }

   @Scheduled(fixedRate = INTERVAL, initialDelayString = "\${random.long($INTERVAL)}")
   fun doDaily() {
      ...
   }
}
Milo van der Zee
  • 1,031
  • 11
  • 30
0

The shortest way I found is:

@Scheduled(initialDelayString = "#{T(Math).round(T(Math).random()*600000)}")
Mert Ülkgün
  • 124
  • 1
  • 7
-6

Or you could just add Thread.sleep(...) at the end of your function.

@Scheduled(fixedDelay = 10000)
public void replicateData() {

    ... do stuff ...

    try {
        Thread.sleep(RandomUtils.nextLong(1000, 10000));
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Vincnetas
  • 146
  • 1
  • 4
  • 1
    It would need to be a random delay at the start of the function. And there would need to be code to make that delay only the first time. That might work but probably a bit clunky – troymass May 19 '17 at 17:11