如何在 Quartz 调度程序中每 25 秒运行一次?

发布于 2024-11-14 07:43:09 字数 156 浏览 8 评论 0原文

我正在使用 Java 的 Quartz Scheduling API。你能帮我使用 cron 表达式每 25 秒运行一次吗?这只是一个延迟。它不必总是从第 0 秒开始。例如,序列如下:0:00、0:25、0:50、1:15、1:40、2:05 等,直到第 5 分钟序列再次从第 0 秒开始。 谢谢。

I am using the Quartz Scheduling API for Java. Could you help me to run every 25 seconds using cron-expression. It's just a delay. It does not have to start always at second 0. For example, the sequence is like this: 0:00, 0:25, 0:50, 1:15, 1:40, 2:05, etc until minute 5 when the sequence begins again at second 0.
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

纸短情长 2024-11-21 07:43:09

我不认为 cron 表达式允许你这样做,但你可以使用

SimpleScheduleBuilder.repeatSecondlyForever( 25 )

300(5 分钟)是 25 的倍数,它会自动重复。

I don't think cron expression will allow you to do that, but you can use

SimpleScheduleBuilder.repeatSecondlyForever( 25 )

as 300 (5 minutes) is a multiple of 25 it will repeat automatically.

2024-11-21 07:43:09

如果您希望定期触发作业,则可以使用指定了 repeatInterval 的 Quartz SimpleTrigger

If you want a job to trigger at a regular interval then you can use a Quartz SimpleTrigger with a repeatInterval specified.

还不是爱你 2024-11-21 07:43:09

使用 Quartz 2.1.5 这将有助于:

CronTrigger trigger = newTrigger()
    .withIdentity("trigger1", "group1")
    .withSchedule(cronSchedule("0/20 * * * * ?"))
    .build();

With Quartz 2.1.5 this will help:

CronTrigger trigger = newTrigger()
    .withIdentity("trigger1", "group1")
    .withSchedule(cronSchedule("0/20 * * * * ?"))
    .build();
凹づ凸ル 2024-11-21 07:43:09

使用 cron 触发器来做到这一点的唯一方法非常复杂,以至于毫无用处;使用其他答案中的 SimpleTrigger 会更好。然而,如果必须是 cron,则需要设置五个不同的 cron 触发器:

 0/25 0/5 * * * *
15/25 1/5 * * * *
 5/25 2/5 * * * *
20/25 3/5 * * * *
10/25 4/5 * * * *

第一个触发器在 0:00:25、0:00:50 触发;第一个触发器在 0:00:25、0:00:50 触发;然后第二个触发器在 0:01:15 和 0:01:40 触发;第三个时间为 0:02:05、0:02:30、0:02:55;第四个在 0:03:20、0:03:45;最后是第五次,时间为 0:04:10 和 0:04:35。然后第一个触发器在 0:05:00 再次接管,依此类推。

这只有效,因为 25 秒均匀地分为 5 分钟(又均匀地分为 1 小时)。如果您想要每 23 秒一次?忘了它吧!

The only way to do this with a cron trigger is so complicated as to be useless; you're much better off with the SimpleTrigger from other answers. Nevertheless, if it has to be cron, you need to set up five different cron triggers:

 0/25 0/5 * * * *
15/25 1/5 * * * *
 5/25 2/5 * * * *
20/25 3/5 * * * *
10/25 4/5 * * * *

The first trigger fires at 0:00:25, 0:00:50; then the second trigger fires at 0:01:15 and 0:01:40; the third at 0:02:05, 0:02:30, 0:02:55; the fourth at 0:03:20, 0:03:45; and finally the fifth at 0:04:10 and 0:04:35. The first trigger then takes over again at 0:05:00, etc.

This only works because 25 seconds divides evenly into 5 minutes (which in turn goes evenly into an hour). If you wanted it every 23 seconds? Forget about it!

×眷恋的温暖 2024-11-21 07:43:09

对于石英来说,你不可能有这样的时间表。

您可以做的一件事是安排包装作业每 5 秒运行一次,并且每五次执行才执行任何工作。

You can't have a schedule like that for quartz.

One thing you could do is schedule a wrapper job to run every 5 seconds, and only do any work every fifth execution.

病毒体 2024-11-21 07:43:09

您可以安排作业不断运行,但使用 Camel 的 Throttler 来限制频率。

<route>
  <from uri="jms:queue:TestQueue"/>
  <!-- throttle 1 messages per 25 sec -->
  <throttle timePeriodMillis="25000">
    <constant>1</constant>
    <to uri="bean:TestBean?method=testMethod"/>
  </throttle>
</route>

You could schedule the job to run constantly but throttle the frequency using Camel's Throttler.

<route>
  <from uri="jms:queue:TestQueue"/>
  <!-- throttle 1 messages per 25 sec -->
  <throttle timePeriodMillis="25000">
    <constant>1</constant>
    <to uri="bean:TestBean?method=testMethod"/>
  </throttle>
</route>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文