Search in sources :

Example 71 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project distributedlog by twitter.

the class TestNonBlockingReads method testNonBlockingReadRecovery.

@Test(timeout = 100000)
public void testNonBlockingReadRecovery() throws Exception {
    String name = "distrlog-non-blocking-reader-recovery";
    final DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.loadConf(conf);
    confLocal.setReadAheadBatchSize(10);
    confLocal.setReadAheadMaxRecords(10);
    final DistributedLogManager dlm = createNewDLM(confLocal, name);
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    ScheduledFuture writerClosedFuture = null;
    try {
        final Thread currentThread = Thread.currentThread();
        writerClosedFuture = executor.schedule(new Runnable() {

            @Override
            public void run() {
                try {
                    writeRecordsForNonBlockingReads(confLocal, dlm, true);
                } catch (Exception exc) {
                    currentThread.interrupt();
                }
            }
        }, 100, TimeUnit.MILLISECONDS);
        readNonBlocking(dlm, false);
        assertFalse(currentThread.isInterrupted());
    } finally {
        if (writerClosedFuture != null) {
            // ensure writer.closeAndComplete is done before we close dlm
            writerClosedFuture.get();
        }
        executor.shutdown();
        dlm.close();
    }
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ScheduledFuture(java.util.concurrent.ScheduledFuture) IdleReaderException(com.twitter.distributedlog.exceptions.IdleReaderException) Test(org.junit.Test)

Example 72 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project graylog2-server by Graylog2.

the class PeriodicalsService method shutDown.

@Override
protected void shutDown() throws Exception {
    for (Periodical periodical : periodicals.getAllStoppedOnGracefulShutdown()) {
        LOG.info("Shutting down periodical [{}].", periodical.getClass().getCanonicalName());
        Stopwatch s = Stopwatch.createStarted();
        // Cancel future executions.
        Map<Periodical, ScheduledFuture> futures = periodicals.getFutures();
        if (futures.containsKey(periodical)) {
            futures.get(periodical).cancel(false);
            s.stop();
            LOG.info("Shutdown of periodical [{}] complete, took <{}ms>.", periodical.getClass().getCanonicalName(), s.elapsed(TimeUnit.MILLISECONDS));
        } else {
            LOG.error("Could not find periodical [{}] in futures list. Not stopping execution.", periodical.getClass().getCanonicalName());
        }
    }
}
Also used : Periodical(org.graylog2.plugin.periodical.Periodical) Stopwatch(com.google.common.base.Stopwatch) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 73 with ScheduledFuture

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

the class AmazonSQSClientMock method scheduleCancelInflight.

/*
     * Cancel (put back onto queue) in flight messages if the visibility time has expired
     * and has not been manually deleted (ack'd)
     */
private void scheduleCancelInflight(final String queueUrl, final Message message) {
    if (scheduler != null) {
        int visibility = getVisibilityForQueue(queueUrl);
        if (visibility > 0) {
            ScheduledFuture task = scheduler.schedule(new Runnable() {

                @Override
                public void run() {
                    synchronized (messages) {
                        // put it back!
                        messages.add(message);
                    }
                }
            }, visibility, TimeUnit.SECONDS);
            inFlight.put(message.getReceiptHandle(), task);
        }
    }
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 74 with ScheduledFuture

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

the class ScheduledThreadPoolExecutorWithKeepAliveJUnitTest method testRepeatedExecution.

@Test
public void testRepeatedExecution() throws InterruptedException {
    ex = new ScheduledThreadPoolExecutorWithKeepAlive(50, 1, TimeUnit.SECONDS, Executors.defaultThreadFactory());
    final AtomicInteger counter = new AtomicInteger();
    Runnable run = new Runnable() {

        public void run() {
            counter.incrementAndGet();
        }
    };
    ScheduledFuture f = ex.scheduleAtFixedRate(run, 0, 1, TimeUnit.SECONDS);
    Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> assertEquals("Task was not executed repeatedly", true, counter.get() > 1));
    Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> assertEquals("The task could not be cancelled", true, f.cancel(true)));
    Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> assertEquals("Task was not cancelled within 30 sec", true, f.isCancelled()));
    int oldValue = counter.get();
    Thread.sleep(5000);
    assertEquals("Task was not cancelled", oldValue, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledFuture(java.util.concurrent.ScheduledFuture) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 75 with ScheduledFuture

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

the class ScheduledThreadPoolExecutorWithKeepAlive method scheduleWithFixedDelay.

public ScheduledFuture scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
    DelegatingScheduledFuture future = new DelegatingScheduledFuture(command, null, true);
    ScheduledFuture timerFuture = timer.scheduleWithFixedDelay(new HandOffTask(future), initialDelay, delay, unit);
    future.setDelegate(timerFuture);
    return future;
}
Also used : 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