简体   繁体   English

是否可以将带有Spring @Scheduled批注的作业安排为每小时运行一次,但每次随机运行一次?

[英]Is it possible to schedule a job with Spring @Scheduled annotation to run every hour but each time at random hour?

I would like to run my task/method each hour.. but each time at random minute. 我想每个小时运行我的任务/方法。但是每次都在随机时间运行。 I've already tried Spring @Scheduled to be started every day at a random minute between 4:00AM and 4:30AM but this solution is setting random initial value but after that same minute is used. 我已经尝试过将Spring @Scheduled每天在4:00 AM到4:30 AM之间的任意分钟启动,但是此解决方案设置了随机初始值,但是使用了同一分钟。

I would like to achieve a situation where the job is running like this. 我想实现一种工作正在这样运行的情况。 ex: 例如:

8:10 9:41 10:12 ... 8:10 9:41 10:12 ...

Right, so...this isn't a schedule. 对,所以...这不是时间表。 This is a non-deterministic event. 这是非确定性事件。

A scheduled event is something which is repeatable and something which can be consistently fired at a specific time. 预定事件是可重复的,并且可以在特定时间一致触发。 There's an order and predictability which go hand-in-hand with this. 有一个顺序和可预测性与此紧密相关。

By having a job fire off at a given hour but not necessarily at a given minute, you lose predictability which is what the @Scheduled annotation would enforce (not necessarily through implementation, but as a side-effect; annotations may only contain compile-time constants and cannot dynamically change during runtime). 通过在给定的时间(但不一定在给定的分钟)解雇工作,您将失去可预测性,这是@Scheduled注释将强制执行的(不一定是通过实现,而是副作用;注释可能仅包含编译时)常量,并且无法在运行时动态更改)。

As for a solution, Thread.sleep is brittle and will cause your entire application to sleep for that period of time which isn't what you want to do. 作为解决方案, Thread.sleep很脆弱,将导致整个应用程序在这段时间内进入睡眠状态,这不是您想要的。 Instead, you could wrap your critical code in a non-blocking thread and schedule that instead. 相反, 您可以将关键代码包装在一个非阻塞线程中,并进行调度。

Warning: Untested code below 警告:下面未经测试的代码

@Scheduled(cron = "0 0 * * * ?")
public void executeStrangely() {
    // Based on the schedule above,
    // all schedule finalization should happen at minute 0.
    // If the pool tries to execute at minute 0, there *might* be
    // a race condition with the actual thread running this block.
    // We do *not* include minute 0 for this reason.
    Random random = new Random();
    final int actualMinuteOfExecution = 1 + random.nextInt(59);
    final ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);

    exec.schedule(() -> {
        // Critical code here
    }, actualMinuteOfExecution, TimeUnit.MINUTES);
}

I leave the effort of managing resources in a thread-safe manner as an exercise for the reader. 我将以线程安全的方式管理资源的工作留给了读者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring @Scheduled(cron =“ 0 * * * *?”)不是每小时运行一次 - Spring @Scheduled(cron = “0 * * * * ?”) not run every hour 如何更改此 Spring Batch @Scheduled() 注释以指定执行时间和时间? - How to change this Spring Batch @Scheduled() annotation in order to specify execution hour and time? 在java线程中每隔一小时运行一个作业 - Run a job for every one hour in java thread 我可以像 CRON 作业一样安排 Java Spring 缓存在每小时的顶部过期吗? - Can I schedule Java Spring Cache to expire at the top of each hour like a CRON job? 每小时安排一次工作,介于50到60分钟之间 - Schedule a job every hour between 50 and 60 minutes once 如何在@Scheduled 注释中指定 PST 时区,该注释应该在太平洋标准时间上午 7 点到下午 6 点之间每小时触发一次? - How to specify PST time zone in @Scheduled annotation that should trigger every hour between 7AM PST to 6 PM PST? 如何在 Java Spring 中的第二个星期一和给定时间安排任务 - How to schedule task every second Monday and given hour in Java Spring 日程安排事件在2小时后运行 - schedule event run after 2 hour 通量反应器 - 每小时简单的时间表 - Flux Reactor - simple schedule every hour 如何安排每小时开始的任务 - How to schedule task for start of every hour
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM