Class BlockingQueueConsumer<T>
- java.lang.Object
-
- org.threadly.util.AbstractService
-
- org.threadly.concurrent.processing.BlockingQueueConsumer<T>
-
- Type Parameters:
T
- Type of items contained in the queue to be consumed
- All Implemented Interfaces:
Service
public abstract class BlockingQueueConsumer<T> extends AbstractService
This class is designed for handling the threading and consumption of a high throughputBlockingQueue
. This class will create a thread to block on for the queue, and provide the result to the abstractaccept(Object)
. For low throughput queues, or where you want threads shared, consider using insteadPoller.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 similarhandleException(Throwable)
will block consuming the next item from the queue.By implementing
Service
this class handles the thread lifecycle. Item consumption will start whenAbstractService.start()
is invoked, and will continue untilAbstractService.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.- Since:
- 5.37 (Former version existed at org.threadly.concurrent)
-
-
Constructor Summary
Constructors Constructor Description 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.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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 newBlockingQueueConsumer
in case an abstract implementation is not preferred.-
Methods inherited from class org.threadly.util.AbstractService
hasStopped, isRunning, start, startIfNotStarted, stop, stopIfRunning
-
-
-
-
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.- Parameters:
threadFactory
- ThreadFactory to construct new thread for consumer to run onqueue
- 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 newBlockingQueueConsumer
in case an abstract implementation is not preferred.- Type Parameters:
T
- The type of object to consume from the queue- Parameters:
threadFactory
- ThreadFactory to construct new thread for consumer to run onqueue
- Queue to consume items fromconsumer
- Consumer to provide items from queue onexceptionHandler
- Handler to send errors to, ifnull
ExceptionUtils.handleException(java.lang.Throwable)
is used- Returns:
- A new
BlockingQueueConsumer
ready tostart()
-
-