Search in sources :

Example 81 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project samza by apache.

the class ScheduleAfterDebounceTime method scheduleAfterDebounceTime.

public synchronized void scheduleAfterDebounceTime(String actionName, long debounceTimeMs, Runnable runnable) {
    // check if this action has been scheduled already
    ScheduledFuture sf = futureHandles.get(actionName);
    if (sf != null && !sf.isDone()) {
        LOG.info("cancel future for " + actionName);
        // attempt to cancel
        if (!sf.cancel(false)) {
            try {
                sf.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
            } catch (Exception e) {
                // we ignore the exception
                LOG.warn("cancel for action " + actionName + " failed with ", e);
            }
        }
        futureHandles.remove(actionName);
    }
    // schedule a new task
    sf = scheduledExecutorService.schedule(() -> {
        try {
            runnable.run();
            LOG.debug(actionName + " completed successfully.");
        } catch (Throwable t) {
            LOG.error(actionName + " threw an exception.", t);
            if (scheduledTaskFailureCallback != null) {
                scheduledTaskFailureCallback.onError(t);
            }
        }
    }, debounceTimeMs, TimeUnit.MILLISECONDS);
    LOG.info("scheduled " + actionName + " in " + debounceTimeMs);
    futureHandles.put(actionName, sf);
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 82 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project samza by apache.

the class TaskCallbackManager method createCallback.

public TaskCallbackImpl createCallback(TaskName taskName, IncomingMessageEnvelope envelope, ReadableCoordinator coordinator) {
    final TaskCallbackImpl callback = new TaskCallbackImpl(listener, taskName, envelope, coordinator, seqNum++, clock.nanoTime());
    if (timer != null) {
        Runnable timerTask = new Runnable() {

            @Override
            public void run() {
                String msg = "Task " + callback.taskName + " callback times out";
                callback.failure(new TaskCallbackTimeoutException(msg));
            }
        };
        ScheduledFuture scheduledFuture = timer.schedule(timerTask, timeout, TimeUnit.MILLISECONDS);
        callback.setScheduledFuture(scheduledFuture);
    }
    return callback;
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 83 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project tomee by apache.

the class ManagedScheduledExecutorServiceImpl method scheduleWithFixedDelay.

@Override
public ScheduledFuture<?> scheduleWithFixedDelay(final Runnable command, final long initialDelay, final long delay, final TimeUnit unit) {
    final CURunnable wrapper = new CURunnable(command);
    final ScheduledFuture<?> future = delegate.scheduleWithFixedDelay(wrapper, initialDelay, 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)

Aggregations

ScheduledFuture (java.util.concurrent.ScheduledFuture)83 Test (org.junit.Test)27 DelegatingScheduledFutureStripper (com.hazelcast.scheduledexecutor.impl.DelegatingScheduledFutureStripper)9 ParallelTest (com.hazelcast.test.annotation.ParallelTest)9 QuickTest (com.hazelcast.test.annotation.QuickTest)9 Date (java.util.Date)9 IOException (java.io.IOException)7 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)7 Map (java.util.Map)6 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 ExecutionException (java.util.concurrent.ExecutionException)4 Future (java.util.concurrent.Future)4