时间范围的 Cron 表达式
我正在使用 Quartz.Net 在我的应用程序中安排我的工作。我只是想知道是否可以构建以下场景的 CRON 表达式:
凌晨 2:15 到 5:20 之间的每一秒
I am using Quartz.Net to schedule my jobs in my application. I was just wondering if a CRON expression for the following scenario can be built:
Every second between 2:15AM and 5:20AM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
robyaw,
非常感谢您的回答。对于如此延迟的回复,我深表歉意。我实际上已经休息了一段时间了。你的解决方案确实有效。我必须在指定的时间范围内创建 3 个 CRON 触发器。您提到的时间范围是正确的。但是,对于您提到的 3 个 CRON 表达式。我担心它们可能无法按预期工作。这些表达式适用于时间范围:2:15AM - 5:20AM - 每天
1)
* 15-59 2 * * ?
- 从 2:15AM 到 3:00AM 每秒,即 2: 15:00AM 至 2:59:59AM2)
* 0-59 3-4 * * ?
- 每秒从 3:00AM 到 5:00AM,即 3:00:00AM 到 4: 59:59AM3)
* 0-19 5 * * ?
- 从 5:00AM 到 5:20AM 每秒,即 5:00:00AM 到 5:19:59AM@gauteh :请注意Quartz .Net 实际上支持第二个触发器。
希望这可以帮助其他可能需要解决类似问题的人。
robyaw,
Thanks a lot for your answer. And I apologize for such a delay in replying. I had actually been off for a while. Your solution indeed works. I had to create 3 CRON triggers for the time range that I had specified. You were right with the time ranges that you had mentioned. However, for the 3 CRON expressions that you had mentioned. I am afraid they might not work as intended. These expressions work for the time range : 2:15AM - 5:20AM - Everyday
1)
* 15-59 2 * * ?
- Every second from 2:15AM to 3:00AM, ie, 2:15:00AM to 2:59:59AM2)
* 0-59 3-4 * * ?
- Every second from 3:00AM to 5:00AM, ie, 3:00:00AM to 4:59:59AM3)
* 0-19 5 * * ?
- Every second from 5:00AM to 5:20AM, ie, 5:00:00AM to 5:19:59AM@gauteh : Please note that Quartz .Net actually supports secondly trigger.
Hope this helps others who might need a solution to a similar problem.
关于 cron 秒支持,UNIX cron 工具之间使用的语法似乎存在一些差异和 CRON 表达式。然而,根据 Quartz CRON 文档,支持秒。
鉴于上述情况,我将创建三个 CRON 触发器来处理:
这将转换为(我相信):
* 15/1 2 * * ?
* * 3-5 * * ?
* 0-20 5 * * ?
Regarding cron seconds support, there appears to be some difference in the syntax used between the UNIX cron tool and CRON Expression. According to the Quartz CRON Documentation however, seconds is supported.
Given the above, I would create three CRON Triggers to handle:
Which would translate to (I believe):
* 15/1 2 * * ?
* * 3-5 * * ?
* 0-20 5 * * ?
这里有一个间隔触发器(每秒),它可以干净地转换为 SimpleTrigger。您需要对其进行限制,仅允许其在特定时间范围(2:15 - 5:20)运行。您可以通过使用日历(更准确地说是 DailyCalendar)来实现这一点。您可以将每日日历设置为具有此时间范围,并将 InvertTimeRange 设置为 true 以包含该范围,而不是默认排除该范围。
在教程和DailyCalendar API 文档。
You have here a interval trigger (every second) that translates cleanly to SimpleTrigger. What you need with it is a restriction to only allow it to run in specific time range (2:15 - 5:20). This you can achieve by using a calendar, more precisely a DailyCalendar. You can set daily calendar to have this time range and set the InvertTimeRange to true to include the range instead of default of excluding the range.
Read more about the calendars in the tutorial and DailyCalendar API documentation.