Class WorkerPool<T>

  • Type Parameters:
    T - event to be processed by a pool of workers

    public final class WorkerPool<T>
    extends java.lang.Object
    WorkerPool contains a pool of WorkProcessors that will consume sequences so jobs can be farmed out across a pool of workers. Each of the WorkProcessors manage and calls a WorkHandler to process the events.
    • Field Detail

      • started

        private final java.util.concurrent.atomic.AtomicBoolean started
      • workSequence

        private final Sequence workSequence
      • workProcessors

        private final WorkProcessor<?>[] workProcessors
    • Constructor Detail

      • WorkerPool

        @SafeVarargs
        public WorkerPool​(RingBuffer<T> ringBuffer,
                          SequenceBarrier sequenceBarrier,
                          ExceptionHandler<? super T> exceptionHandler,
                          WorkHandler<? super T>... workHandlers)
        Create a worker pool to enable an array of WorkHandlers to consume published sequences.

        This option requires a pre-configured RingBuffer which must have RingBuffer.addGatingSequences(Sequence...) called before the work pool is started.

        Parameters:
        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 WorkHandlers.
        workHandlers - to distribute the work load across.
    • Method Detail

      • getWorkerSequences

        public Sequence[] getWorkerSequences()
        Get an array of Sequences representing the progress of the workers.
        Returns:
        an array of Sequences representing the progress of the workers.
      • start

        public RingBuffer<T> start​(java.util.concurrent.Executor executor)
        Start the worker pool processing events in sequence.
        Parameters:
        executor - providing threads for running the workers.
        Returns:
        the RingBuffer used for the work queue.
        Throws:
        java.lang.IllegalStateException - if the pool has already been started and not halted yet
      • drainAndHalt

        public void drainAndHalt()
        Wait for the RingBuffer to drain of published events then halt the workers.
      • halt

        public void halt()
        Halt all workers immediately at the end of their current cycle.
      • isRunning

        public boolean isRunning()