T
- Type of items contained in the queue to be consumedpublic abstract class BlockingQueueConsumer<T> extends AbstractService
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.
Constructor and 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.
|
Modifier and Type | Method and 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 new
BlockingQueueConsumer in case an abstract implementation is not
preferred. |
hasStopped, isRunning, start, startIfNotStarted, stop, stopIfRunning
public BlockingQueueConsumer(java.util.concurrent.ThreadFactory threadFactory, java.util.concurrent.BlockingQueue<? extends T> queue)
threadFactory
- ThreadFactory to construct new thread for consumer to run onqueue
- Queue to consume items frompublic 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)
BlockingQueueConsumer
in case an abstract implementation is not
preferred.T
- The type of object to consume from the queuethreadFactory
- 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, if null
ExceptionUtils.handleException(java.lang.Throwable)
is usedBlockingQueueConsumer
ready to start()