Search in sources :

Example 46 with TriggerContext

use of org.springframework.scheduling.TriggerContext in project nzbhydra2 by theotherp.

the class HydraTaskScheduler method scheduleTask.

private void scheduleTask(TaskRuntimeInformation runtimeInformation, boolean runNow) {
    HydraTask task = runtimeInformation.getMethod().getAnnotation(HydraTask.class);
    if (!taskSchedules.containsKey(task.name())) {
        // On startup
        logger.info("Scheduling task \"{}\" to be run every {}", task.name(), DurationFormatUtils.formatDurationWords(getIntervalForTask(task), true, true));
    }
    Runnable runnable = new ScheduledMethodRunnable(runtimeInformation.getBean(), runtimeInformation.getMethod());
    if (runNow) {
        scheduler.execute(runnable);
    }
    ScheduledFuture scheduledTask = scheduler.schedule(runnable, new Trigger() {

        @Override
        public Date nextExecutionTime(TriggerContext triggerContext) {
            Calendar nextExecutionTime = new GregorianCalendar();
            Date lastActualExecutionTime = runNow ? new Date() : triggerContext.lastActualExecutionTime();
            nextExecutionTime.setTime(lastActualExecutionTime != null ? lastActualExecutionTime : new Date());
            nextExecutionTime.add(Calendar.MILLISECOND, (int) getIntervalForTask(task));
            taskInformations.put(task, new TaskInformation(task.name(), lastActualExecutionTime != null ? lastActualExecutionTime.toInstant() : null, nextExecutionTime.toInstant()));
            return nextExecutionTime.getTime();
        }
    });
    taskSchedules.put(task.name(), scheduledTask);
}
Also used : ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable) Trigger(org.springframework.scheduling.Trigger) TriggerContext(org.springframework.scheduling.TriggerContext) ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 47 with TriggerContext

use of org.springframework.scheduling.TriggerContext in project spring-framework by spring-projects.

the class CronTriggerTests method specificDayOfMonthSecond.

@ParameterizedCronTriggerTest
void specificDayOfMonthSecond(Date localDateTime, TimeZone timeZone) {
    setup(localDateTime, timeZone);
    CronTrigger trigger = new CronTrigger("55 * * 3 * *", timeZone);
    this.calendar.set(Calendar.DAY_OF_MONTH, 2);
    this.calendar.set(Calendar.SECOND, 54);
    Date localDate = this.calendar.getTime();
    TriggerContext context1 = getTriggerContext(localDate);
    this.calendar.add(Calendar.DAY_OF_MONTH, 1);
    this.calendar.set(Calendar.HOUR_OF_DAY, 0);
    this.calendar.set(Calendar.MINUTE, 0);
    this.calendar.set(Calendar.SECOND, 55);
    Object actual1 = localDate = trigger.nextExecutionTime(context1);
    assertThat(actual1).isEqualTo(this.calendar.getTime());
    this.calendar.add(Calendar.MINUTE, 1);
    TriggerContext context2 = getTriggerContext(localDate);
    Object actual = trigger.nextExecutionTime(context2);
    assertThat(actual).isEqualTo(this.calendar.getTime());
}
Also used : TriggerContext(org.springframework.scheduling.TriggerContext) Date(java.util.Date)

Example 48 with TriggerContext

use of org.springframework.scheduling.TriggerContext in project spring-framework by spring-projects.

the class CronTriggerTests method dailyTriggerOnDaylightSavingBoundary.

@ParameterizedCronTriggerTest
void dailyTriggerOnDaylightSavingBoundary(Date localDateTime, TimeZone timeZone) {
    setup(localDateTime, timeZone);
    CronTrigger trigger = new CronTrigger("0 0 0 * * *", timeZone);
    // October: 31 days and a daylight saving boundary in CET
    this.calendar.set(Calendar.MONTH, 9);
    this.calendar.set(Calendar.DAY_OF_MONTH, 30);
    Date localDate = this.calendar.getTime();
    this.calendar.set(Calendar.HOUR_OF_DAY, 0);
    this.calendar.set(Calendar.MINUTE, 0);
    this.calendar.set(Calendar.SECOND, 0);
    this.calendar.set(Calendar.DAY_OF_MONTH, 31);
    TriggerContext context1 = getTriggerContext(localDate);
    Object actual = localDate = trigger.nextExecutionTime(context1);
    assertThat(actual).isEqualTo(this.calendar.getTime());
    // November
    this.calendar.set(Calendar.MONTH, 10);
    this.calendar.set(Calendar.DAY_OF_MONTH, 1);
    TriggerContext context2 = getTriggerContext(localDate);
    assertThat(trigger.nextExecutionTime(context2)).isEqualTo(this.calendar.getTime());
}
Also used : TriggerContext(org.springframework.scheduling.TriggerContext) Date(java.util.Date)

Example 49 with TriggerContext

use of org.springframework.scheduling.TriggerContext in project spring-framework by spring-projects.

the class CronTriggerTests method daylightSavingMissingHour.

@ParameterizedCronTriggerTest
void daylightSavingMissingHour(Date localDateTime, TimeZone timeZone) {
    setup(localDateTime, timeZone);
    // This trigger has to be somewhere between 2:00 AM and 3:00 AM, so we
    // use a cron expression for 2:10 AM every day.
    CronTrigger trigger = new CronTrigger("0 10 2 * * *", timeZone);
    // 2:00 AM on March 31, 2013: start of Daylight Saving Time for CET in 2013.
    // Setting up last completion:
    // - PST: Sun Mar 31 10:09:54 CEST 2013
    // - CET: Sun Mar 31 01:09:54 CET 2013
    this.calendar.set(Calendar.DAY_OF_MONTH, 31);
    this.calendar.set(Calendar.MONTH, Calendar.MARCH);
    this.calendar.set(Calendar.YEAR, 2013);
    this.calendar.set(Calendar.HOUR_OF_DAY, 1);
    this.calendar.set(Calendar.MINUTE, 9);
    this.calendar.set(Calendar.SECOND, 54);
    Date lastCompletionTime = this.calendar.getTime();
    // - CET: Mon Apr 01 02:10:00 CEST 2013
    if (timeZone.equals(TimeZone.getTimeZone("CET"))) {
        // Clocks go forward an hour so 2am doesn't exist in CET for this localDateTime
        this.calendar.add(Calendar.DAY_OF_MONTH, 1);
    }
    this.calendar.add(Calendar.HOUR_OF_DAY, 1);
    this.calendar.set(Calendar.MINUTE, 10);
    this.calendar.set(Calendar.SECOND, 0);
    TriggerContext context = getTriggerContext(lastCompletionTime);
    Object nextExecutionTime = trigger.nextExecutionTime(context);
    assertThat(nextExecutionTime).isEqualTo(this.calendar.getTime());
}
Also used : TriggerContext(org.springframework.scheduling.TriggerContext) Date(java.util.Date)

Example 50 with TriggerContext

use of org.springframework.scheduling.TriggerContext in project spring-framework by spring-projects.

the class CronTriggerTests method nonExistentSpecificDate.

@ParameterizedCronTriggerTest
void nonExistentSpecificDate(Date localDateTime, TimeZone timeZone) {
    setup(localDateTime, timeZone);
    // TODO: maybe try and detect this as a special case in parser?
    CronTrigger trigger = new CronTrigger("0 0 0 31 6 *", timeZone);
    this.calendar.set(Calendar.DAY_OF_MONTH, 10);
    this.calendar.set(Calendar.MONTH, 2);
    Date localDate = this.calendar.getTime();
    TriggerContext context1 = getTriggerContext(localDate);
    assertThat(trigger.nextExecutionTime(context1)).isNull();
}
Also used : TriggerContext(org.springframework.scheduling.TriggerContext) Date(java.util.Date)

Aggregations

TriggerContext (org.springframework.scheduling.TriggerContext)66 Date (java.util.Date)61 Test (org.junit.Test)33 Trigger (org.springframework.scheduling.Trigger)5 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ScheduledFuture (java.util.concurrent.ScheduledFuture)2 Advice (org.aopalliance.aop.Advice)2 AbstractMessageSourceAdvice (org.springframework.integration.aop.AbstractMessageSourceAdvice)2 CompoundTriggerAdvice (org.springframework.integration.aop.CompoundTriggerAdvice)2 SimpleActiveIdleMessageSourceAdvice (org.springframework.integration.aop.SimpleActiveIdleMessageSourceAdvice)2 PollSkipAdvice (org.springframework.integration.scheduling.PollSkipAdvice)2 OnlyOnceTrigger (org.springframework.integration.test.util.OnlyOnceTrigger)2 CompoundTrigger (org.springframework.integration.util.CompoundTrigger)2 DynamicPeriodicTrigger (org.springframework.integration.util.DynamicPeriodicTrigger)2 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)2 ScheduledMethodRunnable (org.springframework.scheduling.support.ScheduledMethodRunnable)2 Method (java.lang.reflect.Method)1 Calendar (java.util.Calendar)1 List (java.util.List)1