Search in sources :

Example 46 with ScheduledExecutorService

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

the class ManagedThrottlerTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    final ScheduledExecutorService badService = new ScheduledThreadPoolExecutor(1) {

        @Override
        public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
            throw new RejectedExecutionException();
        }
    };
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").to("log:foo").throttle(10).id("mythrottler").to("mock:result");
            from("seda:throttleCount").throttle(1).timePeriodMillis(250).id("mythrottler2").to("mock:end");
            from("seda:throttleCountAsync").throttle(1).asyncDelayed().timePeriodMillis(250).id("mythrottler3").to("mock:endAsync");
            from("seda:throttleCountAsyncException").throttle(1).asyncDelayed().timePeriodMillis(250).id("mythrottler4").to("mock:endAsyncException").process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    throw new RuntimeException("Fail me");
                }
            });
            from("seda:throttleCountRejectExecutionCallerRuns").onException(RejectedExecutionException.class).to("mock:rejectedExceptionEndpoint1").end().throttle(1).timePeriodMillis(250).asyncDelayed().executorService(badService).callerRunsWhenRejected(true).id("mythrottler5").to("mock:endAsyncRejectCallerRuns");
            from("seda:throttleCountRejectExecution").onException(RejectedExecutionException.class).to("mock:rejectedExceptionEndpoint1").end().throttle(1).timePeriodMillis(250).asyncDelayed().executorService(badService).callerRunsWhenRejected(false).id("mythrottler6").to("mock:endAsyncReject");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) TimeUnit(java.util.concurrent.TimeUnit) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 47 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project redisson by redisson.

the class RedissonBoundedBlockingQueueTest method testOfferTimeout.

@Test
public void testOfferTimeout() throws InterruptedException {
    RBoundedBlockingQueue<Integer> queue = redisson.getBoundedBlockingQueue("bounded-queue");
    queue.trySetCapacity(5);
    queue.add(1);
    queue.add(2);
    queue.add(3);
    queue.add(4);
    queue.add(5);
    long start = System.currentTimeMillis();
    assertThat(queue.offer(6, 2, TimeUnit.SECONDS)).isFalse();
    assertThat(System.currentTimeMillis() - start).isGreaterThan(1900);
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
    final AtomicBoolean executed = new AtomicBoolean();
    executor.schedule(new Runnable() {

        @Override
        public void run() {
            RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("bounded-queue");
            assertThat(queue1.remove()).isEqualTo(1);
            executed.set(true);
        }
    }, 1, TimeUnit.SECONDS);
    start = System.currentTimeMillis();
    assertThat(queue.offer(6, 3, TimeUnit.SECONDS)).isTrue();
    assertThat(System.currentTimeMillis() - start).isBetween(1000L, 2000L);
    await().atMost(2, TimeUnit.SECONDS).until(() -> assertThat(executed.get()).isTrue());
    assertThat(queue).containsExactly(2, 3, 4, 5, 6);
    executor.shutdown();
    assertThat(executor.awaitTermination(1, TimeUnit.MINUTES)).isTrue();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RBoundedBlockingQueue(org.redisson.api.RBoundedBlockingQueue) Test(org.junit.Test)

Example 48 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project hive by apache.

the class ReplChangeManager method scheduleCMClearer.

// Schedule CMClearer thread. Will be invoked by metastore
public static void scheduleCMClearer(HiveConf hiveConf) {
    if (HiveConf.getBoolVar(hiveConf, HiveConf.ConfVars.REPLCMENABLED)) {
        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new BasicThreadFactory.Builder().namingPattern("cmclearer-%d").daemon(true).build());
        executor.scheduleAtFixedRate(new CMClearer(hiveConf.get(HiveConf.ConfVars.REPLCMDIR.varname), hiveConf.getTimeVar(ConfVars.REPLCMRETIAN, TimeUnit.SECONDS), hiveConf), 0, hiveConf.getTimeVar(ConfVars.REPLCMINTERVAL, TimeUnit.SECONDS), TimeUnit.SECONDS);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService)

Example 49 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project storm by apache.

the class FileBasedEventLogger method setUpFlushTask.

private void setUpFlushTask() {
    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    Runnable task = new Runnable() {

        @Override
        public void run() {
            try {
                if (dirty) {
                    eventLogWriter.flush();
                    dirty = false;
                }
            } catch (IOException ex) {
                LOG.error("Error flushing " + eventLogPath, ex);
                throw new RuntimeException(ex);
            }
        }
    };
    scheduler.scheduleAtFixedRate(task, FLUSH_INTERVAL_MILLIS, FLUSH_INTERVAL_MILLIS, TimeUnit.MILLISECONDS);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) IOException(java.io.IOException)

Example 50 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project elasticsearch by elastic.

the class CacheTests method testDependentKeyDeadlock.

public void testDependentKeyDeadlock() throws BrokenBarrierException, InterruptedException {
    class Key {

        private final int key;

        Key(int key) {
            this.key = key;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o)
                return true;
            if (o == null || getClass() != o.getClass())
                return false;
            Key key1 = (Key) o;
            return key == key1.key;
        }

        @Override
        public int hashCode() {
            return key % 2;
        }
    }
    int numberOfThreads = randomIntBetween(2, 32);
    final Cache<Key, Integer> cache = CacheBuilder.<Key, Integer>builder().build();
    CopyOnWriteArrayList<ExecutionException> failures = new CopyOnWriteArrayList<>();
    CyclicBarrier barrier = new CyclicBarrier(1 + numberOfThreads);
    CountDownLatch deadlockLatch = new CountDownLatch(numberOfThreads);
    List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < numberOfThreads; i++) {
        Thread thread = new Thread(() -> {
            try {
                try {
                    barrier.await();
                } catch (BrokenBarrierException | InterruptedException e) {
                    throw new AssertionError(e);
                }
                Random random = new Random(random().nextLong());
                for (int j = 0; j < numberOfEntries; j++) {
                    Key key = new Key(random.nextInt(numberOfEntries));
                    try {
                        cache.computeIfAbsent(key, k -> {
                            if (k.key == 0) {
                                return 0;
                            } else {
                                Integer value = cache.get(new Key(k.key / 2));
                                return value != null ? value : 0;
                            }
                        });
                    } catch (ExecutionException e) {
                        failures.add(e);
                        break;
                    }
                }
            } finally {
                // successfully avoided deadlock, release the main thread
                deadlockLatch.countDown();
            }
        });
        threads.add(thread);
        thread.start();
    }
    AtomicBoolean deadlock = new AtomicBoolean();
    assert !deadlock.get();
    // start a watchdog service
    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    scheduler.scheduleAtFixedRate(() -> {
        Set<Long> ids = threads.stream().map(t -> t.getId()).collect(Collectors.toSet());
        ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
        long[] deadlockedThreads = mxBean.findDeadlockedThreads();
        if (!deadlock.get() && deadlockedThreads != null) {
            for (long deadlockedThread : deadlockedThreads) {
                // ensure that we detected deadlock on our threads
                if (ids.contains(deadlockedThread)) {
                    deadlock.set(true);
                    // release the main test thread to fail the test
                    for (int i = 0; i < numberOfThreads; i++) {
                        deadlockLatch.countDown();
                    }
                    break;
                }
            }
        }
    }, 1, 1, TimeUnit.SECONDS);
    // everything is setup, release the hounds
    barrier.await();
    // wait for either deadlock to be detected or the threads to terminate
    deadlockLatch.await();
    // shutdown the watchdog service
    scheduler.shutdown();
    assertThat(failures, is(empty()));
    assertFalse("deadlock", deadlock.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Random(java.util.Random) ArrayList(java.util.ArrayList) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) HashSet(java.util.HashSet) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ESTestCase(org.elasticsearch.test.ESTestCase) ManagementFactory(java.lang.management.ManagementFactory) Before(org.junit.Before) CyclicBarrier(java.util.concurrent.CyclicBarrier) Matchers.empty(org.hamcrest.Matchers.empty) Set(java.util.Set) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ThreadMXBean(java.lang.management.ThreadMXBean) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Matchers.is(org.hamcrest.Matchers.is) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Random(java.util.Random) ExecutionException(java.util.concurrent.ExecutionException) ThreadMXBean(java.lang.management.ThreadMXBean) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)332 Test (org.junit.Test)99 ExecutorService (java.util.concurrent.ExecutorService)41 Test (org.testng.annotations.Test)35 CountDownLatch (java.util.concurrent.CountDownLatch)33 IOException (java.io.IOException)31 ArrayList (java.util.ArrayList)31 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)31 HashMap (java.util.HashMap)30 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 Map (java.util.Map)26 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)25 List (java.util.List)19 ThreadFactory (java.util.concurrent.ThreadFactory)16 None (com.linkedin.common.util.None)15 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)15 UUID (java.util.UUID)15 DefaultStatisticsProvider (org.apache.jackrabbit.oak.stats.DefaultStatisticsProvider)15 URI (java.net.URI)14 CompletableFuture (java.util.concurrent.CompletableFuture)14