问题
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:
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.
回答2:
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:
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()}")
回答4:
Keep in mind, that the initialDelayString is evaluated only once at startup and than this same values is used whenever the job is scheduled.
See org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor#processScheduled
回答5:
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() {
...
}
}
回答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();
}
}
来源:https://stackoverflow.com/questions/27869800/spring-scheduled-annotation-random-delay