0

Using Quartz 2.3.0

As the title asks:
Is there a way to get or compute the previously scheduled fire time for a Quartz Trigger from the Job.execute(JobExecutionContext context) method?

I know I can use JobExecutionContext.getScheduledFireTime() to get the scheduled time that this trigger was currently fired for, but I want the scheduled time of the previous fire. Another way to put it: getFireTime() is to getScheduledFireTime() as getPreviousFireTime() is to what?

I am using JobStoreTX and need to remain database agnostic; It would be quite a pain to override all the JDBC Delegates and the Job Store implementations to store and expose the previous scheduled fire time.

The closest I've come is that some of the Trigger implementations, such as SimpleTriggerImpl have a method:

/**
 * <p>
 * Returns the last time at which the <code>SimpleTrigger</code> will
 * fire, before the given time. If the trigger will not fire before the
 * given time, <code>null</code> will be returned.
 * </p>
 */
public Date getFireTimeBefore(Date end);

But CronTriggerImpl has:

/**
 * NOT YET IMPLEMENTED: Returns the time before the given time
 * that this <code>CronTrigger</code> will fire.
 */ 
protected Date getTimeBefore(Date eTime) {
    return (cronEx == null) ? null : cronEx.getTimeBefore(eTime);
}

and unfortunately I will primarily be using CronTrigger, and there is no method like this in the Trigger interface.

Any ideas?

Thanks.


My backup idea is, at the start of each job execution, transactionally reading the previous scheduled time from a separate table and writing the new current scheduled time to that table indexed by SchedulerName, TriggerGroup, and TriggerName.

xtratic
  • 4,600
  • 2
  • 14
  • 32
  • 1
    would this answer be relevant ? https://stackoverflow.com/questions/2710883/finding-last-fired-time-using-a-cron-expression-in-java – Eirini Graonidou Aug 21 '18 at 15:14
  • @EiriniGraonidou relevant, but the answers do not solve my problem. Using an interval to try to calculate the previous scheduled time is not correct since schedules are not necessarily regular. – xtratic Aug 21 '18 at 15:22

1 Answers1

0

Perhaps the class org.quartz.TriggerUtils is the solution for your problem.

Have a look at TriggerUtils.computeFireTimesBetween().

Aldo
  • 536
  • 5
  • 23
  • Unfortunately `computeFireTimesBetween` does not help primarily because the documentation says `"NOTE: if this is a trigger that has previously fired within the given date range, then firings which have already occurred will not be listed in the output List."` – xtratic Aug 21 '18 at 19:15
  • But also my only known low-date would be my start-date and for a long running process computing all schedule times from start to current would get more and more expensive. If the job runs every hour then after a year that would already be computing a next date 8760 times (which isn't exactly cheap). Though I suppose (if it actually yielded the dates..) then I could've computed from start-date once, cached the discovered prev scheduled date, then compute from that prev scheduled date each subsequent time. – xtratic Aug 21 '18 at 19:20