英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论