use of java.util.concurrent.ScheduledFuture in project tomee by apache.
the class ManagedScheduledExecutorServiceImpl method schedule.
@Override
public ScheduledFuture<?> schedule(final Runnable runnable, final Trigger trigger) {
final Date taskScheduledTime = new Date();
final AtomicReference<Future<?>> futureHandle = new AtomicReference<Future<?>>();
final TriggerRunnable wrapper = new TriggerRunnable(this, runnable, new CURunnable(runnable), trigger, taskScheduledTime, getTaskId(runnable), AtomicReference.class.cast(futureHandle));
final ScheduledFuture<?> future = delegate.schedule(wrapper, trigger.getNextRunTime(wrapper.getLastExecution(), taskScheduledTime).getTime() - nowMs(), TimeUnit.MILLISECONDS);
return initTriggerScheduledFuture(runnable, AtomicReference.class.cast(futureHandle), wrapper, ScheduledFuture.class.cast(future));
}
use of java.util.concurrent.ScheduledFuture in project tomee by apache.
the class ManagedScheduledExecutorServiceImpl method schedule.
@Override
public ScheduledFuture<?> schedule(final Runnable command, final long delay, final TimeUnit unit) {
final CURunnable wrapper = new CURunnable(command);
final ScheduledFuture<?> future = delegate.schedule(wrapper, delay, unit);
wrapper.taskSubmitted(future, this, command);
return new CUScheduleFuture<Object>(ScheduledFuture.class.cast(future), wrapper);
}
use of java.util.concurrent.ScheduledFuture in project tomee by apache.
the class ManagedScheduledExecutorServiceImpl method scheduleAtFixedRate.
@Override
public ScheduledFuture<?> scheduleAtFixedRate(final Runnable command, final long initialDelay, final long period, final TimeUnit unit) {
final CURunnable wrapper = new CURunnable(command);
final ScheduledFuture<?> future = delegate.scheduleAtFixedRate(wrapper, initialDelay, period, unit);
wrapper.taskSubmitted(future, this, command);
return new CUScheduleFuture<Object>(ScheduledFuture.class.cast(future), wrapper);
}
use of java.util.concurrent.ScheduledFuture in project tomee by apache.
the class ManagedScheduledExecutorServiceImpl method schedule.
@Override
public <V> ScheduledFuture<V> schedule(final Callable<V> vCallable, final Trigger trigger) {
final Date taskScheduledTime = new Date();
final AtomicReference<Future<V>> futureHandle = new AtomicReference<Future<V>>();
final TriggerCallable<V> wrapper = new TriggerCallable<>(this, vCallable, new CUCallable<>(vCallable), trigger, taskScheduledTime, getTaskId(vCallable), futureHandle);
final ScheduledFuture<V> future = delegate.schedule(wrapper, trigger.getNextRunTime(wrapper.getLastExecution(), taskScheduledTime).getTime() - nowMs(), TimeUnit.MILLISECONDS);
return initTriggerScheduledFuture(vCallable, futureHandle, wrapper, future);
}
use of java.util.concurrent.ScheduledFuture in project tomee by apache.
the class TriggerTask method invoke.
public T invoke() throws Exception {
return invoke(new Callable<T>() {
@Override
public T call() throws Exception {
final long wait = nextDelay(trigger.getNextRunTime(lastExecution, scheduledTime));
if (wait > 0) {
Thread.sleep(wait);
}
// else if wait < 0 then ??
final Date now = new Date();
try {
final boolean skip = trigger.skipRun(lastExecution, now);
if (!skip) {
result = doInvoke();
taskDone(future, executor, delegate, null);
lastExecution = new LastExecutionImpl(id, result, scheduledTime, now, new Date());
} else {
result = null;
skipped = true;
running.set(false);
}
} catch (final RuntimeException re) {
final SkippedException skippedException = new SkippedException(re);
taskAborted(skippedException);
throw skippedException;
}
final ScheduledFuture<T> future = executorService.schedule(this, trigger.getNextRunTime(lastExecution, scheduledTime).getTime() - ManagedScheduledExecutorServiceImpl.nowMs(), TimeUnit.MILLISECONDS);
futureRef.set(future);
taskSubmitted(future, executorService, delegate);
return result;
}
});
}
Aggregations