Search in sources :

Example 16 with Trigger

use of org.springframework.scheduling.Trigger in project uPortal by Jasig.

the class DelegatingThreadPoolTaskScheduler method schedule.

@Override
public ScheduledFuture<?> schedule(Runnable task, final Trigger trigger) {
    task = wrapRunnable(task);
    // Wrap the trigger so that the first call to nextExecutionTime adds in the
    // additionalStartDelay
    final Trigger wrappedTrigger = new Trigger() {

        boolean firstExecution = false;

        @Override
        public Date nextExecutionTime(TriggerContext triggerContext) {
            Date nextExecutionTime = trigger.nextExecutionTime(triggerContext);
            if (nextExecutionTime == null) {
                return null;
            }
            if (firstExecution) {
                nextExecutionTime = getDelayedStartDate(nextExecutionTime);
                firstExecution = true;
            }
            return nextExecutionTime;
        }
    };
    final DelegatingRunnable delegatingRunnable = new DelegatingRunnable(this.executorService, task);
    @SuppressWarnings("unchecked") final ScheduledFuture<?> future = super.schedule(delegatingRunnable, wrappedTrigger);
    return (ScheduledFuture<ScheduledFuture<?>>) new DelegatingForwardingScheduledFuture(future);
}
Also used : Trigger(org.springframework.scheduling.Trigger) TriggerContext(org.springframework.scheduling.TriggerContext) Date(java.util.Date) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 17 with Trigger

use of org.springframework.scheduling.Trigger 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)

Aggregations

Trigger (org.springframework.scheduling.Trigger)17 Test (org.junit.Test)14 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)13 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)10 CronTrigger (org.springframework.scheduling.support.CronTrigger)8 TriggerContext (org.springframework.scheduling.TriggerContext)7 Date (java.util.Date)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 SourcePollingChannelAdapter (org.springframework.integration.endpoint.SourcePollingChannelAdapter)5 OnlyOnceTrigger (org.springframework.integration.test.util.OnlyOnceTrigger)5 Expression (org.springframework.expression.Expression)4 CompoundTriggerAdvice (org.springframework.integration.aop.CompoundTriggerAdvice)4 PollerMetadata (org.springframework.integration.scheduling.PollerMetadata)4 CompoundTrigger (org.springframework.integration.util.CompoundTrigger)4 DynamicPeriodicTrigger (org.springframework.integration.util.DynamicPeriodicTrigger)4 ArrayList (java.util.ArrayList)3 Advice (org.aopalliance.aop.Advice)3 AbstractMessageSourceAdvice (org.springframework.integration.aop.AbstractMessageSourceAdvice)3 SimpleActiveIdleMessageSourceAdvice (org.springframework.integration.aop.SimpleActiveIdleMessageSourceAdvice)3 PollSkipAdvice (org.springframework.integration.scheduling.PollSkipAdvice)3