问题
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.
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?
回答1:
Inside your job use the following code:
@Override
public void execute(final JobExecutionContext context) {
long diffInMillis =
new Date().getTime() - context.getScheduledFireTime().getTime();
//...
}
As you probably guessed context.getScheduledFireTime()
is the time when job was suppose to run (ideally the difference should be close to 0).
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.
来源:https://stackoverflow.com/questions/12249994/difference-between-last-actual-and-scheduled-fire-time