SpringBoot的 @Scheduled 注解,每两次执行之间的延迟是随机的。

huangapple go评论69阅读模式
英文:

SpringBoot @Scheduled random delay between every two executions

问题

我对SpringBoot相当新,正在尝试使用@Scheduled来执行某个带有延迟的任务。
我知道有fixedDelay参数,可以设置两次连续执行之间的固定延迟。

我需要的是,是否有任何方法可以在每两次执行之间设置随机延迟?(我的意思是每个延迟都是随机设置的,而不是对fixedDelay设置随机延迟)

我能想到的是,使用fixedDelay,再加上执行过程中随机秒数的Thread.sleep(),但感觉应该有更正确的方法来做到这一点。

英文:

I'm fairly new to SpringBoot, and trying to use @Scheduled to execute a certain task with delays.
I know there is fixedDelay parameter that can set to have a fixed delay between two executions in a row.

What I need is, is there anyway to set a random delay between every two executions? (I mean every delay is randomly set, but not having a random delay set to fixedDelay)

What I can come up with is, use a fixedDelay, plus a random seconds of Thread.sleep() inside the execution, but feel like there should be a more correct way to do this.

答案1

得分: 0

似乎没有办法使用Spring的API来实现这个。

最终我创建了两个调度,分别使用了两个不同的fixedDelay,它们都执行相同的任务,例如:第一个延迟27秒,而第二个延迟43秒,因此目标任务似乎以随机延迟执行,而不是固定的延迟。

英文:

Seems like there is no way to make this happen by using spring's api.

I ended up with making 2 schedules with 2 different fixedDelay and both of them execute the same task, e.g: first one with delay of 27 sec while the second has 43 sec, so it looks like the target task being executed with a random delay rather than a fixed one.

huangapple
  • 本文由 发表于 2020年8月25日 17:21:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63575784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定