@Test public void withSameHourRange() { new CronSequenceGenerator("* * 6-6 * * *").next(new Date(2012, 6, 1, 9, 0)); }
/** * Determine whether the specified expression represents a valid cron pattern. * @param expression the expression to evaluate * @return {@code true} if the given expression is a valid cron expression * @since 4.3 */ public static boolean isValidExpression(@Nullable String expression) { if (expression == null) { return false; } String[] fields = StringUtils.tokenizeToStringArray(expression, " "); if (!areValidCronFields(fields)) { return false; } try { new CronSequenceGenerator(expression, fields); return true; } catch (IllegalArgumentException ex) { return false; } }
int updateSecond = findNext(this.seconds, second, calendar, Calendar.SECOND, Calendar.MINUTE, emptyList); if (second == updateSecond) { resets.add(Calendar.SECOND); int updateMinute = findNext(this.minutes, minute, calendar, Calendar.MINUTE, Calendar.HOUR_OF_DAY, resets); if (minute == updateMinute) { resets.add(Calendar.MINUTE); doNext(calendar, dot); int updateHour = findNext(this.hours, hour, calendar, Calendar.HOUR_OF_DAY, Calendar.DAY_OF_WEEK, resets); if (hour == updateHour) { resets.add(Calendar.HOUR_OF_DAY); doNext(calendar, dot); int updateDayOfMonth = findNextDay(calendar, this.daysOfMonth, dayOfMonth, this.daysOfWeek, dayOfWeek, resets); if (dayOfMonth == updateDayOfMonth) { resets.add(Calendar.DAY_OF_MONTH); doNext(calendar, dot); int updateMonth = findNext(this.months, month, calendar, Calendar.MONTH, Calendar.YEAR, resets); if (month != updateMonth) { if (calendar.get(Calendar.YEAR) - dot > 4) { doNext(calendar, dot);
/** * Parse the given pattern expression. */ private void parse(String expression) throws IllegalArgumentException { String[] fields = StringUtils.tokenizeToStringArray(expression, " "); if (!areValidCronFields(fields)) { throw new IllegalArgumentException(String.format( "Cron expression must consist of 6 fields (found %d in \"%s\")", fields.length, expression)); } doParse(fields); }
/** * Build a {@link CronTrigger} from the pattern provided in the default time zone. * @param expression a space-separated list of time fields, following cron * expression conventions */ public CronTrigger(String expression) { this.sequenceGenerator = new CronSequenceGenerator(expression); }
/** * 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 CronSequenceGenerator(String expression, String[] fields) { this.expression = expression; this.timeZone = null; doParse(fields); }
@Override public boolean equals(Object other) { return (this == other || (other instanceof CronTrigger && this.sequenceGenerator.equals(((CronTrigger) other).sequenceGenerator))); }
/** * Build a {@link CronTrigger} from the pattern provided in the given time zone. * @param expression a space-separated list of time fields, following cron * expression conventions * @param timeZone a time zone in which the trigger times will be generated */ public CronTrigger(String expression, TimeZone timeZone) { this.sequenceGenerator = new CronSequenceGenerator(expression, timeZone); }
/** * 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); }
/** * Parse the given pattern expression. */ private void parse(String expression) throws IllegalArgumentException { String[] fields = StringUtils.tokenizeToStringArray(expression, " "); if (!areValidCronFields(fields)) { throw new IllegalArgumentException(String.format( "Cron expression must consist of 6 fields (found %d in \"%s\")", fields.length, expression)); } doParse(fields); }
private CronSequenceGenerator(String expression, String[] fields) { this.expression = expression; this.timeZone = null; doParse(fields); }
@Override public boolean equals(Object other) { return (this == other || (other instanceof CronTrigger && this.sequenceGenerator.equals(((CronTrigger) other).sequenceGenerator))); }
@Test(expected = IllegalArgumentException.class) public void withInvertedMinuteRange() { new CronSequenceGenerator("* 6-5 * * * *").next(new Date(2012, 6, 1, 9, 0)); }
/** * Build a {@link CronTrigger} from the pattern provided in the given time zone. * @param expression a space-separated list of time fields, following cron * expression conventions * @param timeZone a time zone in which the trigger times will be generated */ public CronTrigger(String expression, TimeZone timeZone) { this.sequenceGenerator = new CronSequenceGenerator(expression, timeZone); }
/** * Determine whether the specified expression represents a valid cron pattern. * @param expression the expression to evaluate * @return {@code true} if the given expression is a valid cron expression * @since 4.3 */ public static boolean isValidExpression(@Nullable String expression) { if (expression == null) { return false; } String[] fields = StringUtils.tokenizeToStringArray(expression, " "); if (!areValidCronFields(fields)) { return false; } try { new CronSequenceGenerator(expression, fields); return true; } catch (IllegalArgumentException ex) { return false; } }
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); }
int updateSecond = findNext(this.seconds, second, calendar, Calendar.SECOND, Calendar.MINUTE, emptyList); if (second == updateSecond) { resets.add(Calendar.SECOND); int updateMinute = findNext(this.minutes, minute, calendar, Calendar.MINUTE, Calendar.HOUR_OF_DAY, resets); if (minute == updateMinute) { resets.add(Calendar.MINUTE); doNext(calendar, dot); int updateHour = findNext(this.hours, hour, calendar, Calendar.HOUR_OF_DAY, Calendar.DAY_OF_WEEK, resets); if (hour == updateHour) { resets.add(Calendar.HOUR_OF_DAY); doNext(calendar, dot); int updateDayOfMonth = findNextDay(calendar, this.daysOfMonth, dayOfMonth, this.daysOfWeek, dayOfWeek, resets); if (dayOfMonth == updateDayOfMonth) { resets.add(Calendar.DAY_OF_MONTH); doNext(calendar, dot); int updateMonth = findNext(this.months, month, calendar, Calendar.MONTH, Calendar.YEAR, resets); if (month != updateMonth) { if (calendar.get(Calendar.YEAR) - dot > 4) { doNext(calendar, dot);