Search in sources :

Example 66 with BlockingQueue

use of java.util.concurrent.BlockingQueue in project roof-im by madfroglx.

the class RedisBlockingQueueLoadBalanceMessagePublisher method createQueues.

@Override
protected List<BlockingQueue> createQueues(String serverName) {
    List<BlockingQueue> list = new ArrayList<>();
    for (RedisTemplate redisTemplate : redisTemplates) {
        BoundListOperations boundListOperations = redisTemplate.boundListOps(serverName);
        BlockingQueue blockingQueue = new DefaultRedisList(boundListOperations);
        list.add(blockingQueue);
    }
    return list;
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) BoundListOperations(org.springframework.data.redis.core.BoundListOperations) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) DefaultRedisList(org.springframework.data.redis.support.collections.DefaultRedisList) ArrayList(java.util.ArrayList)

Example 67 with BlockingQueue

use of java.util.concurrent.BlockingQueue in project opennms by OpenNMS.

the class LegacyScheduler method run.

/**
 * The main method of the scheduler. This method is responsible for checking
 * the runnable queues for ready objects and then enqueuing them into the
 * thread pool for execution.
 */
@Override
public void run() {
    synchronized (this) {
        m_status = RUNNING;
    }
    LOG.debug("run: scheduler running");
    /*
         * Loop until a fatal exception occurs or until
         * the thread is interrupted.
         */
    for (; ; ) {
        /*
             * Block if there is nothing in the queue(s).
             * When something is added to the queue it
             * signals us to wakeup.
             */
        synchronized (this) {
            if (m_status != RUNNING && m_status != PAUSED && m_status != PAUSE_PENDING && m_status != RESUME_PENDING) {
                LOG.debug("run: status = {}, time to exit", m_status);
                break;
            }
            // if paused or pause pending then block
            while (m_status == PAUSE_PENDING || m_status == PAUSED) {
                if (m_status == PAUSE_PENDING) {
                    LOG.debug("run: pausing.");
                }
                m_status = PAUSED;
                try {
                    wait();
                } catch (InterruptedException ex) {
                    // exit
                    break;
                }
            }
            if (m_status == RESUME_PENDING) {
                LOG.debug("run: resuming.");
                m_status = RUNNING;
            }
            if (m_scheduled == 0) {
                try {
                    LOG.debug("run: no ready runnables scheduled, waiting...");
                    wait();
                } catch (InterruptedException ex) {
                    break;
                }
            }
        }
        /*
             * Cycle through the queues checking for
             * what's ready to run.  The queues are keyed
             * by the interval, but the mapped elements
             * are peekable fifo queues.
             */
        int runned = 0;
        synchronized (m_queues) {
            /*
                 * Get an iterator so that we can cycle
                 * through the queue elements.
                 */
            for (Entry<Long, BlockingQueue<ReadyRunnable>> entry : m_queues.entrySet()) {
                /*
                     * Peak for Runnable objects until
                     * there are no more ready runnables.
                     *
                     * Also, only go through each queue once!
                     * if we didn't add a count then it would
                     * be possible to starve other queues.
                     */
                BlockingQueue<ReadyRunnable> in = entry.getValue();
                ReadyRunnable readyRun = null;
                int maxLoops = in.size();
                do {
                    try {
                        readyRun = in.peek();
                        if (readyRun != null && readyRun.isReady()) {
                            LOG.debug("run: found ready runnable {}", readyRun);
                            /*
                                 * Pop the interface/readyRunnable from the
                                 * queue for execution.
                                 */
                            in.take();
                            // Add runnable to the execution queue
                            m_runner.execute(readyRun);
                            ++runned;
                            // Increment the execution counter
                            ++m_numTasksExecuted;
                            // Thread Pool Statistics
                            if (m_runner instanceof ThreadPoolExecutor) {
                                ThreadPoolExecutor e = (ThreadPoolExecutor) m_runner;
                                String ratio = String.format("%.3f", e.getTaskCount() > 0 ? new Double(e.getCompletedTaskCount()) / new Double(e.getTaskCount()) : 0);
                                LOG.debug("thread pool statistics: activeCount={}, taskCount={}, completedTaskCount={}, completedRatio={}, poolSize={}", e.getActiveCount(), e.getTaskCount(), e.getCompletedTaskCount(), ratio, e.getPoolSize());
                            }
                        }
                    } catch (InterruptedException e) {
                        // jump all the way out
                        return;
                    } catch (RejectedExecutionException e) {
                        throw new UndeclaredThrowableException(e);
                    }
                } while (readyRun != null && readyRun.isReady() && --maxLoops > 0);
            }
        }
        /*
             * Wait for 1 second if there were no runnables
             * executed during this loop, otherwise just
             * start over.
             */
        synchronized (this) {
            m_scheduled -= runned;
            if (runned == 0) {
                try {
                    wait(1000);
                } catch (InterruptedException ex) {
                    // exit for loop
                    break;
                }
            }
        }
    }
    LOG.debug("run: scheduler exiting, state = STOPPED");
    synchronized (this) {
        m_status = STOPPED;
    }
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 68 with BlockingQueue

use of java.util.concurrent.BlockingQueue in project jetty.project by eclipse.

the class ServerTimeoutsTest method testNoBlockingTimeoutBlockingWriteIdleTimeoutFires.

@Test
public void testNoBlockingTimeoutBlockingWriteIdleTimeoutFires() throws Exception {
    httpConfig.setBlockingTimeout(-1);
    CountDownLatch handlerLatch = new CountDownLatch(1);
    start(new BlockingWriteHandler(handlerLatch));
    long idleTimeout = 2500;
    setServerIdleTimeout(idleTimeout);
    try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
        BlockingQueue<Callback> callbacks = new LinkedBlockingQueue<>();
        CountDownLatch resultLatch = new CountDownLatch(1);
        client.newRequest(newURI()).onResponseContentAsync((response, content, callback) -> {
            // Do not succeed the callback so the server will block writing.
            callbacks.offer(callback);
        }).send(result -> {
            if (result.isFailed())
                resultLatch.countDown();
        });
        // Blocking write should timeout.
        Assert.assertTrue(handlerLatch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
        // After the server stopped sending, consume on the client to read the early EOF.
        while (true) {
            Callback callback = callbacks.poll(1, TimeUnit.SECONDS);
            if (callback == null)
                break;
            callback.succeeded();
        }
        Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
    }
}
Also used : BadMessageException(org.eclipse.jetty.http.BadMessageException) Request(org.eclipse.jetty.server.Request) ServletException(javax.servlet.ServletException) HttpChannel(org.eclipse.jetty.server.HttpChannel) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletInputStream(javax.servlet.ServletInputStream) AbstractHTTP2ServerConnectionFactory(org.eclipse.jetty.http2.server.AbstractHTTP2ServerConnectionFactory) TimeoutException(java.util.concurrent.TimeoutException) ByteBuffer(java.nio.ByteBuffer) AsyncContext(javax.servlet.AsyncContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) WriteListener(javax.servlet.WriteListener) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpStatus(org.eclipse.jetty.http.HttpStatus) Callback(org.eclipse.jetty.util.Callback) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ReadListener(javax.servlet.ReadListener) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Assert(org.junit.Assert) Callback(org.eclipse.jetty.util.Callback) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Example 69 with BlockingQueue

use of java.util.concurrent.BlockingQueue in project hbase by apache.

the class TestAsyncTable method testSimpleMultiple.

@Test
public void testSimpleMultiple() throws Exception {
    AsyncTableBase table = getTable.get();
    int count = 100;
    CountDownLatch putLatch = new CountDownLatch(count);
    IntStream.range(0, count).forEach(i -> table.put(new Put(concat(row, i)).addColumn(FAMILY, QUALIFIER, concat(VALUE, i))).thenAccept(x -> putLatch.countDown()));
    putLatch.await();
    BlockingQueue<Boolean> existsResp = new ArrayBlockingQueue<>(count);
    IntStream.range(0, count).forEach(i -> table.exists(new Get(concat(row, i)).addColumn(FAMILY, QUALIFIER)).thenAccept(x -> existsResp.add(x)));
    for (int i = 0; i < count; i++) {
        assertTrue(existsResp.take());
    }
    BlockingQueue<Pair<Integer, Result>> getResp = new ArrayBlockingQueue<>(count);
    IntStream.range(0, count).forEach(i -> table.get(new Get(concat(row, i)).addColumn(FAMILY, QUALIFIER)).thenAccept(x -> getResp.add(Pair.newPair(i, x))));
    for (int i = 0; i < count; i++) {
        Pair<Integer, Result> pair = getResp.take();
        assertArrayEquals(concat(VALUE, pair.getFirst()), pair.getSecond().getValue(FAMILY, QUALIFIER));
    }
    CountDownLatch deleteLatch = new CountDownLatch(count);
    IntStream.range(0, count).forEach(i -> table.delete(new Delete(concat(row, i))).thenAccept(x -> deleteLatch.countDown()));
    deleteLatch.await();
    IntStream.range(0, count).forEach(i -> table.exists(new Get(concat(row, i)).addColumn(FAMILY, QUALIFIER)).thenAccept(x -> existsResp.add(x)));
    for (int i = 0; i < count; i++) {
        assertFalse(existsResp.take());
    }
    IntStream.range(0, count).forEach(i -> table.get(new Get(concat(row, i)).addColumn(FAMILY, QUALIFIER)).thenAccept(x -> getResp.add(Pair.newPair(i, x))));
    for (int i = 0; i < count; i++) {
        Pair<Integer, Result> pair = getResp.take();
        assertTrue(pair.getSecond().isEmpty());
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) ClientTests(org.apache.hadoop.hbase.testclassification.ClientTests) Supplier(java.util.function.Supplier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestName(org.junit.rules.TestName) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Parameterized(org.junit.runners.Parameterized) Bytes(org.apache.hadoop.hbase.util.Bytes) Pair(org.apache.hadoop.hbase.util.Pair) Before(org.junit.Before) TableName(org.apache.hadoop.hbase.TableName) AfterClass(org.junit.AfterClass) MediumTests(org.apache.hadoop.hbase.testclassification.MediumTests) Parameter(org.junit.runners.Parameterized.Parameter) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) UncheckedIOException(java.io.UncheckedIOException) ExecutionException(java.util.concurrent.ExecutionException) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) HBaseTestingUtility(org.apache.hadoop.hbase.HBaseTestingUtility) Assert.assertFalse(org.junit.Assert.assertFalse) ForkJoinPool(java.util.concurrent.ForkJoinPool) Assert.assertEquals(org.junit.Assert.assertEquals) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Pair(org.apache.hadoop.hbase.util.Pair) Test(org.junit.Test)

Example 70 with BlockingQueue

use of java.util.concurrent.BlockingQueue in project mapdb by jankotek.

the class BlockingQueueTest method testTimedPollFromEmptyBlocksInterruptibly.

/**
     * timed poll() blocks interruptibly when empty
     */
public void testTimedPollFromEmptyBlocksInterruptibly() {
    final BlockingQueue q = emptyCollection();
    final CountDownLatch threadStarted = new CountDownLatch(1);
    Thread t = newStartedThread(new CheckedRunnable() {

        public void realRun() {
            threadStarted.countDown();
            try {
                q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
                shouldThrow();
            } catch (InterruptedException success) {
            }
            assertFalse(Thread.interrupted());
        }
    });
    await(threadStarted);
    assertThreadStaysAlive(t);
    t.interrupt();
    awaitTermination(t);
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

BlockingQueue (java.util.concurrent.BlockingQueue)129 Test (org.junit.Test)59 CountDownLatch (java.util.concurrent.CountDownLatch)21 LocalConcurrentBlockingObjectQueue (net.openhft.chronicle.sandbox.queue.LocalConcurrentBlockingObjectQueue)21 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)18 ArrayList (java.util.ArrayList)12 Ignore (org.junit.Ignore)12 IOException (java.io.IOException)10 BlockingQueueTest (net.openhft.chronicle.sandbox.queue.common.BlockingQueueTest)10 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)9 TimeUnit (java.util.concurrent.TimeUnit)9 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)8 ByteBuffer (java.nio.ByteBuffer)4 List (java.util.List)4 Assert (org.junit.Assert)4 SynchronousQueue (java.util.concurrent.SynchronousQueue)3 TimeoutException (java.util.concurrent.TimeoutException)3 AsyncContext (javax.servlet.AsyncContext)3 ReadListener (javax.servlet.ReadListener)3 ServletException (javax.servlet.ServletException)3