春季@计划每天在凌晨4:00 AM和4:30 AM之间的任意分钟开始 [英] Spring @Scheduled to be started every day at a random minute between 4:00AM and 4:30AM
问题描述
如问题标题中所述,我需要设置Spring Scheduler,它将运行方法将每天从数据库中的内容加载到内存中,每天凌晨4点左右
as stated in the title of a question, I need to set Spring Scheduler that will run method to load something from database into memory, every day around 4AM
问题是我有该服务器的多个实例,并且我不希望所有实例都同时开始执行,因为这会降低数据库的速度。所以我希望时间是在凌晨4:00 AM到4:30 AM之间的某个随机时间
The thing is that I have multiple instances of this server and I don't want all to start executing at the same time cause it will slow down the DB. So I want the time to be at a random minute somewhere between 4:00AM and 4:30AM
所以可以说一个实例每天开始于4:03 AM,另一个实例每天开始查询执行持续1分钟。
So lets say one instance will start everyday at 4:03AM, the other at 4:09AM, third at 4:21AM etc. The execution of query lasts for 1 minute.
这可能与cron表达式有关,但不使用$ RANDOM bash(因为我想我没有),或者也许我需要以其他方式将这个随机值注入
Is this possible to do with cron expression, but without using $RANDOM bash (cause I think I dont have it) , or maybe I need to inject this random value some other way into
@Scheduled(cron="* randomMinuteValue 4 * * *")
推荐答案
使用Spring的 RandomValuePropertySource
@Scheduled(cron="0 ${random.int[0,30]} 4 * * ?")
这篇关于春季@计划每天在凌晨4:00 AM和4:30 AM之间的任意分钟开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!