上次实际点火时间与预定点火时间之间的差异 [英] difference between last actual and scheduled fire time
问题描述
我想获得作业应该执行的时间与最近一次执行的实际执行时间之间的延迟.例如,如果某个作业应该在晚上 8 点开工,而实际上在晚上 8.10 开工,那么结果应该是 10 分钟.
I want to get the delay between the time a job was supposed to execute and the time it actually executed for the most recent execution. For example, if a job was supposed to fire at 8pm and it actually fired at 8.10pm, the result should be 10 minutes.
我知道我可以使用 Trigger.getPreviousFireTime() 获取它实际执行的最后一次,但是我看不到任何方法来获取相应的预定时间(例如上面示例中的晚上 8 点),是这样吗可能吗?
I know that I can use Trigger.getPreviousFireTime() to get the last time it actually executed, but I can't see any way to get the corresponding scheduled time (e.g. 8pm for the example above), is this possible?
推荐答案
在你的工作中使用以下代码:
Inside your job use the following code:
@Override
public void execute(final JobExecutionContext context) {
long diffInMillis =
new Date().getTime() - context.getScheduledFireTime().getTime();
//...
}
正如您可能猜到的,context.getScheduledFireTime()
是作业应该运行的时间(理想情况下,差异应该接近 0).
As you probably guessed context.getScheduledFireTime()
is the time when job was suppose to run (ideally the difference should be close to 0).
请注意,如果您的作业延迟超过 10 分钟(可配置),它可能根本不会触发 - 这取决于您的失火指令设置.
Note that if your job is late more than 10 minutes (configurable) it might not fire at all - it depends on your misfire instruction set up.
这篇关于上次实际点火时间与预定点火时间之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!