/** * Create a new {@code CronTask}. * @param runnable the underlying task to execute * @param expression the cron expression defining when the task should be executed */ public CronTask(Runnable runnable, String expression) { this(runnable, new CronTrigger(expression)); }
/** * 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); }
/** * Build a message for an invocation failure exception. * @return the error message, including the target method name etc */ protected String getInvocationFailureMessage() { return "Invocation of method '" + getTargetMethod() + "' on target class [" + getTargetClass() + "] failed"; }
@Test public void fixedRateWithInitialDelayFirstExecution() { Date now = new Date(); long period = 5000; long initialDelay = 30000; PeriodicTrigger trigger = new PeriodicTrigger(period); trigger.setFixedRate(true); trigger.setInitialDelay(initialDelay); Date next = trigger.nextExecutionTime(context(null, null, null)); assertApproximateDifference(now, next, initialDelay); }
@Test(expected = IllegalArgumentException.class) public void testNonExistentSpecificDate() throws Exception { // TODO: maybe try and detect this as a special case in parser? CronTrigger trigger = new CronTrigger("0 0 0 31 6 *", timeZone); calendar.set(Calendar.DAY_OF_MONTH, 10); calendar.set(Calendar.MONTH, 2); Date date = calendar.getTime(); TriggerContext context1 = getTriggerContext(date); trigger.nextExecutionTime(context1); // new CronTrigger("0 0 0 30 1 ?", timeZone); }
@Test public void fixedDelaySubsequentExecution() { Date now = new Date(); long period = 5000; PeriodicTrigger trigger = new PeriodicTrigger(period); Date next = trigger.nextExecutionTime(context(now, 500, 3000)); assertApproximateDifference(now, next, period + 3000); }
private static TriggerContext getTriggerContext(Date lastCompletionTime) { SimpleTriggerContext context = new SimpleTriggerContext(); context.update(null, null, lastCompletionTime); return context; }
private Runnable errorHandlingTask(Runnable task, boolean isRepeatingTask) { return TaskUtils.decorateTaskWithErrorHandler(task, this.errorHandler, isRepeatingTask); }
/** * Create a new {@code CronTask}. * @param runnable the underlying task to execute * @param cronTrigger the cron trigger defining when the task should be executed */ public CronTask(Runnable runnable, CronTrigger cronTrigger) { super(runnable, cronTrigger); this.expression = cronTrigger.getExpression(); }
/** * Return the cron pattern that this trigger has been built with. */ public String getExpression() { return this.sequenceGenerator.getExpression(); }
/** * Construct a {@link CronSequenceGenerator} from the pattern provided, * using the specified {@link TimeZone}. * @param expression a space-separated list of time fields * @param timeZone the TimeZone to use for generated trigger times * @throws IllegalArgumentException if the pattern cannot be parsed */ public CronSequenceGenerator(String expression, TimeZone timeZone) { this.expression = expression; this.timeZone = timeZone; parse(expression); }
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))); }
@Override public String toString() { return this.sequenceGenerator.toString(); }
@Override public void afterPropertiesSet() throws ClassNotFoundException, NoSuchMethodException { prepare(); }
@Override public int hashCode() { return this.sequenceGenerator.hashCode(); }
private static TriggerContext context(Object scheduled, Object actual, Object completion) { return new TestTriggerContext(asDate(scheduled), asDate(actual), asDate(completion)); }
@Test public void fixedRateWithInitialDelaySubsequentExecution() { Date now = new Date(); long period = 5000; long initialDelay = 30000; PeriodicTrigger trigger = new PeriodicTrigger(period); trigger.setFixedRate(true); trigger.setInitialDelay(initialDelay); Date next = trigger.nextExecutionTime(context(now, 500, 3000)); assertApproximateDifference(now, next, period); }
/** * 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); }
private Runnable errorHandlingTask(Runnable delegate, boolean isRepeatingTask) { return TaskUtils.decorateTaskWithErrorHandler(delegate, this.errorHandler, isRepeatingTask); }