T
- event to be processed by a pool of workerspublic final class WorkerPool<T>
extends java.lang.Object
WorkProcessor
s that will consume sequences so jobs can be farmed out across a pool of workers.
Each of the WorkProcessor
s manage and calls a WorkHandler
to process the events.Modifier and Type | Field and Description |
---|---|
private RingBuffer<T> |
ringBuffer |
private java.util.concurrent.atomic.AtomicBoolean |
started |
private WorkProcessor<?>[] |
workProcessors |
private Sequence |
workSequence |
Constructor and Description |
---|
WorkerPool(EventFactory<T> eventFactory,
ExceptionHandler<? super T> exceptionHandler,
WorkHandler<? super T>... workHandlers)
Construct a work pool with an internal
RingBuffer for convenience. |
WorkerPool(RingBuffer<T> ringBuffer,
SequenceBarrier sequenceBarrier,
ExceptionHandler<? super T> exceptionHandler,
WorkHandler<? super T>... workHandlers)
Create a worker pool to enable an array of
WorkHandler s to consume published sequences. |
Modifier and Type | Method and Description |
---|---|
void |
drainAndHalt()
Wait for the
RingBuffer to drain of published events then halt the workers. |
Sequence[] |
getWorkerSequences()
Get an array of
Sequence s representing the progress of the workers. |
void |
halt()
Halt all workers immediately at the end of their current cycle.
|
boolean |
isRunning() |
RingBuffer<T> |
start(java.util.concurrent.Executor executor)
Start the worker pool processing events in sequence.
|
private final java.util.concurrent.atomic.AtomicBoolean started
private final Sequence workSequence
private final RingBuffer<T> ringBuffer
private final WorkProcessor<?>[] workProcessors
public WorkerPool(RingBuffer<T> ringBuffer, SequenceBarrier sequenceBarrier, ExceptionHandler<? super T> exceptionHandler, WorkHandler<? super T>... workHandlers)
WorkHandler
s to consume published sequences.
This option requires a pre-configured RingBuffer
which must have RingBuffer.addGatingSequences(Sequence...)
called before the work pool is started.
ringBuffer
- of events to be consumed.sequenceBarrier
- on which the workers will depend.exceptionHandler
- to callback when an error occurs which is not handled by the WorkHandler
s.workHandlers
- to distribute the work load across.public WorkerPool(EventFactory<T> eventFactory, ExceptionHandler<? super T> exceptionHandler, WorkHandler<? super T>... workHandlers)
RingBuffer
for convenience.
This option does not require RingBuffer.addGatingSequences(Sequence...)
to be called before the work pool is started.
eventFactory
- for filling the RingBuffer
exceptionHandler
- to callback when an error occurs which is not handled by the WorkHandler
s.workHandlers
- to distribute the work load across.public Sequence[] getWorkerSequences()
Sequence
s representing the progress of the workers.Sequence
s representing the progress of the workers.public RingBuffer<T> start(java.util.concurrent.Executor executor)
executor
- providing threads for running the workers.RingBuffer
used for the work queue.java.lang.IllegalStateException
- if the pool has already been started and not halted yetpublic void drainAndHalt()
RingBuffer
to drain of published events then halt the workers.public void halt()
public boolean isRunning()