use of org.apache.nifi.controller.tasks.ConnectableTask in project nifi by apache.
the class QuartzSchedulingAgent method doSchedule.
@Override
public synchronized void doSchedule(final Connectable connectable, final LifecycleState scheduleState) {
final List<AtomicBoolean> existingTriggers = canceledTriggers.get(connectable);
if (existingTriggers != null) {
throw new IllegalStateException("Cannot schedule " + connectable + " because it is already scheduled to run");
}
final String cronSchedule = connectable.getSchedulingPeriod();
final CronExpression cronExpression;
try {
cronExpression = new CronExpression(cronSchedule);
} catch (final Exception pe) {
throw new IllegalStateException("Cannot schedule " + connectable + " to run because its scheduling period is not valid");
}
final List<AtomicBoolean> triggers = new ArrayList<>();
for (int i = 0; i < connectable.getMaxConcurrentTasks(); i++) {
final ConnectableTask continuallyRunTask = new ConnectableTask(this, connectable, flowController, contextFactory, scheduleState, encryptor);
final AtomicBoolean canceled = new AtomicBoolean(false);
final Runnable command = new Runnable() {
@Override
public void run() {
if (canceled.get()) {
return;
}
try {
continuallyRunTask.invoke();
} catch (final RuntimeException re) {
throw re;
} catch (final Exception e) {
throw new ProcessException(e);
}
if (canceled.get()) {
return;
}
final Date date = cronExpression.getTimeAfter(new Date());
final long delay = date.getTime() - System.currentTimeMillis();
logger.debug("Finished task for {}; next scheduled time is at {} after a delay of {} milliseconds", connectable, date, delay);
flowEngine.schedule(this, delay, TimeUnit.MILLISECONDS);
}
};
final Date initialDate = cronExpression.getTimeAfter(new Date());
final long initialDelay = initialDate.getTime() - System.currentTimeMillis();
flowEngine.schedule(command, initialDelay, TimeUnit.MILLISECONDS);
triggers.add(canceled);
}
canceledTriggers.put(connectable, triggers);
logger.info("Scheduled {} to run with {} threads on schedule {}", connectable, connectable.getMaxConcurrentTasks(), cronSchedule);
}
use of org.apache.nifi.controller.tasks.ConnectableTask in project nifi by apache.
the class TimerDrivenSchedulingAgent method doSchedule.
@Override
public void doSchedule(final Connectable connectable, final LifecycleState scheduleState) {
final List<ScheduledFuture<?>> futures = new ArrayList<>();
final ConnectableTask connectableTask = new ConnectableTask(this, connectable, flowController, contextFactory, scheduleState, encryptor);
for (int i = 0; i < connectable.getMaxConcurrentTasks(); i++) {
// Determine the task to run and create it.
final AtomicReference<ScheduledFuture<?>> futureRef = new AtomicReference<>();
final Runnable trigger = createTrigger(connectableTask, scheduleState, futureRef);
// Schedule the task to run
final ScheduledFuture<?> future = flowEngine.scheduleWithFixedDelay(trigger, 0L, connectable.getSchedulingPeriod(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS);
// now that we have the future, set the atomic reference so that if the component is yielded we
// are able to then cancel this future.
futureRef.set(future);
// Keep track of the futures so that we can update the ScheduleState.
futures.add(future);
}
scheduleState.setFutures(futures);
logger.info("Scheduled {} to run with {} threads", connectable, connectable.getMaxConcurrentTasks());
}
Aggregations