24

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.

1

8 Answers 8

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.

4
  • 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
    Commented Jan 10, 2015 at 5: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
    Commented Jan 10, 2015 at 7: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
    Commented Jun 8, 2015 at 15:26
  • Confirmed with multiple Spring versions that this does not work
    – anupash
    Commented Aug 1, 2018 at 21:19
20

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

3
  • 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
    Commented Jul 12, 2016 at 2:03
  • 2
    Confirmed with multiple Spring versions that this does not work
    – anupash
    Commented Aug 1, 2018 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. Commented Jul 28, 2021 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()}")
3

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

1
  • 1
    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
    Commented Jan 23, 2017 at 21:36
2

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();
}
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() {
      ...
   }
}
0

The shortest way I found is:

@Scheduled(initialDelayString = "#{T(Math).round(T(Math).random()*600000)}")
-5

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();
    }
}
1
  • 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
    Commented May 19, 2017 at 17:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.