EJB 3.0 TimerService - 在每月的第一天运行计时器
在 EJB 3.0 中,TimerService.createTimer(initialDuration, IntervalDuration, TimerID)
方法仅接受 initialDuration
和 intervalDuration
参数。
问题是我想在每个月的 1 号运行计时器,但无法使用 EJB 3.1 TimerService
中的 cron 表达式设置计时器。
一些解决方法是每天运行并检查 @Timeout
方法中的日期(如果是“第一个月”),但这不是合适的方法。
我在网上浏览了很多示例/教程,但没有运气。还有其他方法可以实现这个场景吗?
In EJB 3.0 TimerService.createTimer(initialDuration, intervalDuration, TimerID)
method is accepting only initialDuration
and intervalDuration
arguments.
The problem is I want to run timer on 1st of every month and I can not set my timer using cron expression like in EJB 3.1 TimerService
.
Some workaround is like run on daily base and check the date inside @Timeout
method if its '1st of month' but that is not appropriate way.
I went through many example/tutorials online but no luck. Is there any other way to implement this scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议采用以下方法:
@Timeout
方法中使用 JodaTime 来计算下一个时间点。因此,每个计时器都会在其@Timeout
方法中安排下一次调用。这样做的优点是,您可以构建一个管理对话框,提供系统中计划计时器的概述,如果您每天运行计时器,则无法做到这一点。我用它来实现一个相当复杂的、用户可配置的计时器配置(Outlook 风格,即“每两个月的第三个上午 9:00 运行一次)”。即使使用 JodaTime,我最终还是编写了大量代码,不过,就你的情况来说,基本上不应该那么复杂。I suggest the following approaches:
@Timeout
method use JodaTime to calculate the next point-in-time. So every timer schedules the next call in its@Timeout
method . This has the advantage that you are able to build an admin dialog that provides an overview of the scheduled timers in the system which you can't do if you just run the timer daily. I used this to implement a fairly complex, user-configurable timer configuration (Outlook style, i.e. something like "run this every two months on the third at 9:00 AM). Even with JodaTime, I ended up writing a lot of code, though. However, in your case it shouldn't be that complicated. Basically like使用上面的代码它将在每个月的 1 号运行计时器。
Use above code it will run timer at 1st of every month.