gpt4 book ai didi

jakarta-ee - Java EE 7 : Get @Schedule date when is set to persistent = true

转载 作者:行者123 更新时间:2023-12-03 22:39:21 25 4
gpt4 key购买 nike

在我的 Java EE 7 应用程序中,我创建了一个每小时运行的持久调度任务。我使用了@Schedule 注释。

当这项工作被调用时,我想对从最后一小时到现在发生的事件执行一些操作。要立即获取,我使用“new Date()”。

这个@Schedule 的默认持久属性为“true”,这很好,因为如果服务器在几个小时内停止,我想在它重新启动时执行这些任务。

问题是,如果服务器在 5 小时内停止,作业将启动 5 次,但在所有执行中都会执行“new Date()”,结果是一样的。

有什么方法可以检索应该启动@Schedule 作业的日期???

最佳答案

Is there any way to retrieve the date when the @Schedule job should have been launched???



我认为答案是否定的,没有。

specification定义了三个必须由 Container 实现的接口(interface)。
这些接口(interface)是: TimerService , TimerTimerHandle .
如您所见,其中没有一个(特别是 Timer)公开该方法
你需要的。因此,不强制符合 Java EE 的服务器实现此行为。

可能您应该自己实现一个提供此要求的解决方案。
例如,您可以存储最后一个回调方法时的时间戳
已执行并使用此时间戳来确定时间段(而不是从当前日期减去 1 小时)

...if the server has been stopped during 5 hours, the job will be launched 5 times...



请注意,您不能依赖此

18.4.3 Timer Expiration and Timeout Callback Method ...Any interval persistent timers or schedule based persistent timers that have expired during the intervening time must cause the corresponding timeout callback method to be invoked at least once upon restart.

关于jakarta-ee - Java EE 7 : Get @Schedule date when is set to persistent = true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25929097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com