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);
}
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);
}
Aggregations