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

冷暖自知 提交于 2019-12-05 10:09:18
Gabriel Aramburu

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

I think the answer is no, there isn't.

The specification defines three interfaces that must be implemented by the Container. These interface are: TimerService, Timer and TimerHandle. As you can see, no one of them (specially Timer) exposes the method that you need. Therefore, a Java EE compliant server is not forced to implement this behavior.

Probably you should implement by yourself a solution that provides this requirement. For example, you could store the timestamp when the last callback method was executed and use this timestamp for determining the period of time (instead of subtract 1 hour to the current date)

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

Note that you cannot rely on this

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.

You can reconstruct the time your timer should be actually run by using javax.ejb.Timer:

@Timeout
public void timeout(Timer timer) {
    // get next time
    Date nextTime = timer.getNextTimeout();
    // substract 1 hour
    Calendar c = Calendar.getInstance();
    c.setTime(timer.getNextTimeout());
    c.add(Calendar.HOUR_OF_DAY, -1);
    Date actualTimerDate = c.getTime();
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!