Class BlockingQueueConsumer<T>

  • Type Parameters:
    T - Type of items contained in the queue to be consumed
    All Implemented Interfaces:

    public abstract class BlockingQueueConsumer<T>
    extends AbstractService
    This class is designed for handling the threading and consumption of a high throughput BlockingQueue. This class will create a thread to block on for the queue, and provide the result to the abstract accept(Object). For low throughput queues, or where you want threads shared, consider using instead Poller.consumeQueue(java.util.Queue, Consumer, ExceptionHandler).

    This implementation will create a single thread, serially calling to accept(Object), and not attempting to consume the next item till after it returns. If any errors happen similar handleException(Throwable) will block consuming the next item from the queue.

    By implementing Service this class handles the thread lifecycle. Item consumption will start when AbstractService.start() is invoked, and will continue until AbstractService.stop() is invoked. When stopped, the consumer thread will be interrupted. This is necessary to unblock from consuming from the queue, but may impact the accepting code if it is currently handling an item.

    5.37 (Former version existed at org.threadly.concurrent)
    • Constructor Detail

      • BlockingQueueConsumer

        public BlockingQueueConsumer​(java.util.concurrent.ThreadFactory threadFactory,
                                     java.util.concurrent.BlockingQueue<? extends T> queue)
        Constructs a new consumer, with a provided queue to consume from, and an acceptor to accept items.
        threadFactory - ThreadFactory to construct new thread for consumer to run on
        queue - Queue to consume items from
    • Method Detail

      • makeForHandlers

        public static <T> BlockingQueueConsumer<T> makeForHandlers​(java.util.concurrent.ThreadFactory threadFactory,
                                                                   java.util.concurrent.BlockingQueue<? extends T> queue,
                                                                   java.util.function.Consumer<T> consumer,
                                                                   ExceptionHandler exceptionHandler)
        Construct a new BlockingQueueConsumer in case an abstract implementation is not preferred.
        Type Parameters:
        T - The type of object to consume from the queue
        threadFactory - ThreadFactory to construct new thread for consumer to run on
        queue - Queue to consume items from
        consumer - Consumer to provide items from queue on
        exceptionHandler - Handler to send errors to, if null ExceptionUtils.handleException(java.lang.Throwable) is used
        A new BlockingQueueConsumer ready to start()