/** * Determine the next execution time according to the given trigger context. * <p>Next execution times are calculated based on the * {@linkplain TriggerContext#lastCompletionTime completion time} of the * previous execution; therefore, overlapping executions won't occur. */ @Override public Date nextExecutionTime(TriggerContext triggerContext) { Date date = triggerContext.lastCompletionTime(); if (date != null) { Date scheduled = triggerContext.lastScheduledExecutionTime(); if (scheduled != null && date.before(scheduled)) { // Previous task apparently executed too early... // Let's simply use the last calculated execution time then, // in order to prevent accidental re-fires in the same second. date = scheduled; } } else { date = new Date(); } return this.sequenceGenerator.next(date); }
private void submitNext() { long currentTime = clock.currentTimeMillis(); Date nextSchedule = generator.next(new Date(currentTime)); long delay = nextSchedule.getTime() - currentTime; if (logger.isDebugEnabled()) { logger.debug("Now={}, next audit time={}, delay={} ms", new Date(currentTime), nextSchedule, delay); } trigger.schedule(new Runnable() { @Override public void run() { try { auditor.execute(); } catch (CacheClosedException e) { logger.warn("Cache closed while attempting to rebalance the cluster. Abort future jobs", e); return; } catch (Exception e) { logger.warn("Error while executing out-of-balance audit.", e); } submitNext(); } }, delay, TimeUnit.MILLISECONDS); }
/** * Determine the next execution time according to the given trigger context. * <p>Next execution times are calculated based on the * {@linkplain TriggerContext#lastCompletionTime completion time} of the * previous execution; therefore, overlapping executions won't occur. */ @Override public Date nextExecutionTime(TriggerContext triggerContext) { Date date = triggerContext.lastCompletionTime(); if (date != null) { Date scheduled = triggerContext.lastScheduledExecutionTime(); if (scheduled != null && date.before(scheduled)) { // Previous task apparently executed too early... // Let's simply use the last calculated execution time then, // in order to prevent accidental re-fires in the same second. date = scheduled; } } else { date = new Date(); } return this.sequenceGenerator.next(date); }
@Test(expected = IllegalArgumentException.class) public void with0Increment() { new CronSequenceGenerator("*/0 * * * * *").next(new Date(2012, 6, 1, 9, 0)); }
@Test(expected = IllegalArgumentException.class) public void withInvertedMinuteRange() { new CronSequenceGenerator("* 6-5 * * * *").next(new Date(2012, 6, 1, 9, 0)); }
@Test public void withSameMinuteRange() { new CronSequenceGenerator("* 6-6 * * * *").next(new Date(2012, 6, 1, 9, 0)); }
@Test public void withSameHourRange() { new CronSequenceGenerator("* * 6-6 * * *").next(new Date(2012, 6, 1, 9, 0)); }
@Test(expected = IllegalArgumentException.class) public void withNegativeIncrement() { new CronSequenceGenerator("*/-1 * * * * *").next(new Date(2012, 6, 1, 9, 0)); }
@Test(expected = IllegalArgumentException.class) public void withInvertedHourRange() { new CronSequenceGenerator("* * 6-5 * * *").next(new Date(2012, 6, 1, 9, 0)); }
@Test public void at50Seconds() { assertEquals(new Date(2012, 6, 2, 1, 0), new CronSequenceGenerator("*/15 * 1-4 * * *").next(new Date(2012, 6, 1, 9, 53, 50))); }
@Test public void at0Minutes() { assertEquals(new Date(2012, 6, 2, 1, 0), new CronSequenceGenerator("0 */2 1-4 * * *").next(new Date(2012, 6, 1, 9, 0))); }
@Test public void at0Seconds() { assertEquals(new Date(2012, 6, 2, 1, 0), new CronSequenceGenerator("*/15 * 1-4 * * *").next(new Date(2012, 6, 1, 9, 53))); }
CronSequenceGenerator generator = new CronSequenceGenerator(cronExpression); Date nextRunDate= generator.next(new Date());
@Override public long nextInvocationDelaySeconds(Date currentTime, Date startTime, Date lastInvocationTime, int pastInvocatonsCount) { Date nextInvocationTime; if (lastInvocationTime == null) { nextInvocationTime = cronSequenceGenerator.next(startTime); } else { nextInvocationTime = cronSequenceGenerator.next(lastInvocationTime); } long resultMilliseconds = nextInvocationTime.getTime() - currentTime.getTime(); if (resultMilliseconds < 0) { resultMilliseconds = 0; } if (currentTime.getTime() + resultMilliseconds >= expiration.getTime()) { return FlowConstants.NONE; } return resultMilliseconds / SECOND; }
@Override public long nextInvocationDelaySeconds(Date currentTime, Date startTime, Date lastInvocationTime, int pastInvocatonsCount) { Date nextInvocationTime; if (lastInvocationTime == null) { nextInvocationTime = cronSequenceGenerator.next(startTime); } else { nextInvocationTime = cronSequenceGenerator.next(lastInvocationTime); } long resultMilliseconds = nextInvocationTime.getTime() - currentTime.getTime(); if (resultMilliseconds < 0) { resultMilliseconds = 0; } if (currentTime.getTime() + resultMilliseconds >= expiration.getTime()) { return FlowConstants.NONE; } return resultMilliseconds / SECOND; }
private LocalDateTime calculateNextStart(String schedule) { Date next = new CronSequenceGenerator(schedule).next(new Date()); return LocalDateTime.ofInstant(next.toInstant(), ZoneId.systemDefault()); }
CronSequenceGenerator generator = new CronSequenceGenerator("0 0 8 */1 * *"); Date next = generator.next(prev);
protected long calculateNextCronDate(ScheduledTask task, long date, long currentDate, long frame) { CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(task.getCron(), getCurrentTimeZone()); //if last start = 0 (task never has run) or to far in the past, we use (NOW - FRAME) timestamp for pivot time //this approach should work fine cause cron works with absolute time long pivotPreviousTime = Math.max(date, currentDate - frame); Date currentStart = null; Date nextDate = cronSequenceGenerator.next(new Date(pivotPreviousTime)); while (nextDate.getTime() < currentDate) {//if next date is in past try to find next date nearest to now currentStart = nextDate; nextDate = cronSequenceGenerator.next(nextDate); } if (currentStart == null) { currentStart = nextDate; } log.trace("{}\n now={} frame={} currentStart={} lastStart={} cron={}", task, currentDate, frame, currentStart, task.getCron()); return currentStart.getTime(); }
public static Date next(String expression, Date from) { CronSequenceGenerator generator = new CronSequenceGenerator(normalize(expression, true), Constants.TIMEZONE); return generator.next(from); }
@Test(enabled = false) public void testCronExpression() { CronSequenceGenerator cron1 = new CronSequenceGenerator("0 0 23 31 12 *"); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, 2); // add two days to current date SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss"); log.error("current date " + sdf.format(cal.getTime())); log.error("Next cron trigger date cron1 " + cron1.next(cal.getTime())); } }