使用 Quartz Cron Trigger 处理夏令时的方法
我有一个石英 cron 触发器,如下所示:
<bean id="batchProcessCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="batchProcessJobDetail" />
<property name="cronExpression" value="0 30 2 * * ?" />
</bean>
如果我在凌晨 2-3 点期间发生了多个配置,我应该如何解决这个问题?是否有公认的最佳实践?
相关链接:http://www.quartz-scheduler.org/docs/faq.html#FAQ -daylightSavings
基本上它说的是“处理它”。但我的问题是如何!
I have a quartz cron trigger that looks like so:
<bean id="batchProcessCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="batchProcessJobDetail" />
<property name="cronExpression" value="0 30 2 * * ?" />
</bean>
How should I solve this, if I have several configurations that happen within the 2-3am period? Is there an accepted best practice?
Relevant link: http://www.quartz-scheduler.org/docs/faq.html#FAQ-daylightSavings
Basically it says "Deal with it." But my question is how!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用一个单独的触发器解决了这个问题,该触发器仅在 DST 开始日期触发(提前一小时),用于东部时间凌晨 2 点到 3 点之间发生的配置。
看起来很笨拙,但它确实有效......
I solved it using a separate trigger that only fires (an hour early) on the beginning date of DST for the configurations that happen between 2am and 3am Eastern.
Seems kludgey, but it works...
我们正在使用以下解决方案。为此,您还需要 joda 时间库。
该类的使用方式如下:
这里是一些示例触发时间:
如果您发现此解决方案有任何错误/问题,请发布。
We are using the following solution. For this you will also need the joda time library.
The class is used as follows:
Here some sample trigger times:
Please post if you find any bugs/issues with this solution.
我采纳了 Ron 非常有趣的答案,并改进了 getTimeAfter 方法,以便将其调整为服务器 GMT 运行以及调度“一年一次”cron 表达式时可能存在的差异。
请注意,我的服务器以 GMT 模式运行,因此我不使用 Ron 的答案中存在的一些偏移量转换。
我还发现了一个 Quartz 错误,如果您使用以下配置,它将失败,因为它无法正确处理 cron 表达式:
这实际上似乎发生了,因为温哥华的 DST 更改发生在 3 月 9 日凌晨 2 点,并且似乎super.getTimeAfter(date) 方法的 Quartz 内部实现将始终发送 null。
我希望这些信息有用。
I took Ron's very interesting answer and improved the getTimeAfter Method, In order to adjust it to server GMT running and possible differences when scheduling 'Once a year' cron expressions.
Please note my server runs in GMT mode, therefore I do not use some of the offset conversions present in Ron's answer.
Also I discovered a Quartz bug, in which if you use the following configuration, it will fail because it is not capable of processing the cron expression correctly:
This actually seems to happen because DST change takes place during 9th of March 2am for Vancouver and seems the Quartz internal implementation of the super.getTimeAfter(date) method will always send null.
I hope this information is useful.
我知道这个问题很老了,但它似乎仍然有效。我相信我已经找到了解决这个问题的方法,我将其留在这里,以防其他人偶然发现它并发现它很方便
Spring 5.3 改进了调度,使用 java.time API 重写。它还支持 cron 表达式的特定于quartz 的扩展。
示例计算代码:
相关的spring类是org.springframework.scheduling.support.CronExpression。
详细说明 https:// spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions
@update:Spring 调度程序不支持 cron 表达式中的年份:( 所以它可能不支持在你的场景中工作
I'm aware this question is quite old, but it still seems valid. I believe i've found a way to solve this problem, i'l leave it here in case someone else sumbles upon it and finds it handy
With spring 5.3 comes improved scheduling, rewritten using java.time API. It also supports quartz-specific extensions to cron expressions.
Example computation code:
Relevant spring class is org.springframework.scheduling.support.CronExpression.
Detailed description https://spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions
@update: Spring scheduler doesn't support years in cron expressions :( so it might not work in you scenario