初始延迟的Cron表达式

编程入门 行业动态 更新时间:2024-10-28 04:17:21
本文介绍了初始延迟的Cron表达式 - Quartz的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我只是可以弄清楚如何配置Cron作业在Quartz与初始延迟。 所以我需要每小时运行一次,初始延迟10分钟。

* * 0/1 * *?

解决方案

这是一个迟到的答案,希望这可以帮助别人。我通过在我的服务类中有2个计划的函数来解决这个问题:

@EnableScheduling public class DeviceService { @Scheduled(ini​​tialDelayString =$ {devices.update.initial},fixedDelay = 2592000000L) public void initialUpdateDevices(){ updateDevices() ; } @Scheduled(cron =$ {devices.update.cron}) public void cronUpdateDevices(){ updateDevices(); } private void updateDevices(){ ... } }

初始延迟和cron表达式在application.properties中设置。 fixedDelay是因为Spring不允许initialDelay单独。我设置为2592000000ms,这是30天。在我们的应用程序中,潜在的额外更新不会造成任何损害。

在application.properties:

devices.update.initial = 600000 devices.update.cron = 0 30 1 * * *

最初在10分钟(60000ms)后运行,然后每天晚上在01:30运行。

在用于单元测试的application-test.properties中:

devices.update.initial = 86400000 devices.update.cron = 0 30 1 24 12 *

我们的单元测试都不需要1天执行这样86400000毫秒是一个安全的赌注。 cron0 30 1 24 12 *被设置为圣诞节前夕的夜晚,人们应该做梦的美好的事情。

I am just can figure out how to configure a Cron job in Quartz with initial delay. So i need something that runs every hour with an initial delay of 10 min.

"* * 0/1 * * ?"

解决方案

Here's a late answer, hopefully this helps others. I solved the issue by having 2 scheduled functions in my service class:

@EnableScheduling public class DeviceService { @Scheduled(initialDelayString = "${devices.update.initial}", fixedDelay = 2592000000L) public void initialUpdateDevices() { updateDevices(); } @Scheduled(cron = "${devices.update.cron}") public void cronUpdateDevices() { updateDevices(); } private void updateDevices() { ... } }

The initial delay and the cron expression are set in application.properties. The fixedDelay is there because Spring doesn't allow initialDelay alone. I set it to 2592000000ms, which is 30 days. In our application, the potential extra update doesn't do any harm.

In application.properties:

devices.update.initial = 600000 devices.update.cron = 0 30 1 * * *

Initially run after 10 minutes (60000ms) and then every night at 01:30.

In application-test.properties for unit testing:

devices.update.initial = 86400000 devices.update.cron = 0 30 1 24 12 *

None of our unit tests take 1 day to execute so 86400000 milliseconds is a safe bet. The cron "0 30 1 24 12 *" is set to Christmas Eve's night when people should be dreaming of nice things.

更多推荐

初始延迟的Cron表达式

本文发布于:2023-11-24 08:39:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624504.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   Cron

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!