石英工作调谐
你好,我在工作时用石英实现了一些东西。假设一个 cron 设置为每 2 分钟用表达式 0 0/2 * * *
唤醒? 。
当您在 13:10:30 运行该项目时,第一个操作发生在 13:12:00,第二个操作发生在 13:14:00,其余操作每 2 分 0 秒发生一次。显然,从项目启动到动作第一次发生之间只有 100 万分 30 秒的时间。
有没有办法让第一次出现时尊重 2 分钟,无论项目在哪几秒启动?
hello there is something i've realized with quartz when working.Say a cron is set to wake up every 2min with the expression 0 0/2 * * *
? .
When you run the project at say 13:10:30, the first action happens at 13:12:00 and the second 13:14:00 and every 2min 0 second for the rest. Obviously between the startup of the project and the first occurence of the action there have been 1mn:30s only.
Is there a way to for the first occurrence to respect the 2min no matter which at seconds the project starts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Cron 作业在 Quartz 中使用
CronTrigger
类进行配置。另一种方法是使用SimpleTrigger
,您可以使用固定延迟间隔构建它。SimpleTrigger
有各种构造函数,允许您指定开始时间、结束时间、重复次数、重复间隔等。话虽如此,我建议不要使用 Quartz 进行这种调度,而使用 java.util.concurrent.Executors.newScheduledThreadPool() 。当涉及到简单的重复任务时,它比 Quartz 容易得多。
Cron jobs are configured in Quartz using the
CronTrigger
class. The alternative is to useSimpleTrigger
, which you can construct using fixed delay intervals.SimpleTrigger
has various constructors, allowing you to specify the start time, end time, number of repeats, repeat interval, and so on.Having said that, I'd recommend against using Quartz for this kind of scheduling, and use
java.util.concurrent.Executors.newScheduledThreadPool()
. It's much easier than Quartz when it comes to simple repeating tasks.Quartz 可以使用 cron 进行调度,它基于日期和时间,而不是持续时间。这意味着您定义的 cron 表达式与计算机上的当前时间直接相关,而不是与应用程序启动的时间相关。
我不知道 Quartz 配置可以帮助您解决您的问题。但是,解决方案是创建您自己的
Thread
,该线程在应用程序启动期间启动,并且在调用方法之前基本上等待 2 分钟:Quartz may use cron for the scheduling, which is based on date and time, not duration. This means that the cron expression you define is directly related to the current time on the machine, not on when the application started.
I am not aware of a Quartz configuration that will help you to solve your problem. However, a solution is to create your own
Thread
, which started during the launch of your application and that basically waits 2 minutes before calling a method: