Search in sources :

Example 36 with ScheduledFuture

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));
}
Also used : CURunnable(org.apache.openejb.threads.task.CURunnable) CUScheduleFuture(org.apache.openejb.threads.future.CUScheduleFuture) ScheduledFuture(java.util.concurrent.ScheduledFuture) Future(java.util.concurrent.Future) AtomicReference(java.util.concurrent.atomic.AtomicReference) Date(java.util.Date) TriggerRunnable(org.apache.openejb.threads.task.TriggerRunnable) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 37 with ScheduledFuture

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);
}
Also used : CURunnable(org.apache.openejb.threads.task.CURunnable) CUScheduleFuture(org.apache.openejb.threads.future.CUScheduleFuture) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 38 with ScheduledFuture

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);
}
Also used : CURunnable(org.apache.openejb.threads.task.CURunnable) CUScheduleFuture(org.apache.openejb.threads.future.CUScheduleFuture) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 39 with ScheduledFuture

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);
}
Also used : TriggerCallable(org.apache.openejb.threads.task.TriggerCallable) CUScheduleFuture(org.apache.openejb.threads.future.CUScheduleFuture) ScheduledFuture(java.util.concurrent.ScheduledFuture) Future(java.util.concurrent.Future) AtomicReference(java.util.concurrent.atomic.AtomicReference) Date(java.util.Date)

Example 40 with ScheduledFuture

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;
        }
    });
}
Also used : SkippedException(javax.enterprise.concurrent.SkippedException) SkippedException(javax.enterprise.concurrent.SkippedException) Date(java.util.Date) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Aggregations

ScheduledFuture (java.util.concurrent.ScheduledFuture)85 Test (org.junit.Test)27 Date (java.util.Date)10 DelegatingScheduledFutureStripper (com.hazelcast.scheduledexecutor.impl.DelegatingScheduledFutureStripper)9 ParallelTest (com.hazelcast.test.annotation.ParallelTest)9 QuickTest (com.hazelcast.test.annotation.QuickTest)9 IOException (java.io.IOException)8 Map (java.util.Map)7 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)7 TimeValue (org.elasticsearch.common.unit.TimeValue)6 None (com.linkedin.common.util.None)5 D2Client (com.linkedin.d2.balancer.D2Client)5 D2ClientBuilder (com.linkedin.d2.balancer.D2ClientBuilder)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 JSONObject (org.json.simple.JSONObject)5 IdleReaderException (com.twitter.distributedlog.exceptions.IdleReaderException)4 Future (java.util.concurrent.Future)4 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)4