public class OperationBatcher<T> extends java.lang.Object implements reactor.fn.Supplier<Operation<T>>
OperationBatcher is a highly-efficient data processor that is backed by an LMAX Disruptor RingBuffer.
Rather than dealing with dynamic Consumer registration and event routing, the Processor gains its
extreme efficiency from pre-allocating the data objects (for which you pass a Supplier whose job is to
provide new instances of the data class on startup) and by baking-in a Consumer for published events.
The Processor can be used in two different modes: single-operation or batch. For single operations, use the
prepare() method to allocate an object from the RingBuffer. An Operation is also a Supplier, so call Supplier.get() to get access to the data object. One can then update the
members on the data object and, by calling Operation.commit(), publish the event into
the RingBuffer to be handled by the Consumer.
To operate on the Processor in batch mode, first set a BatchConsumer as the Consumer of
events. This interface provides two additional methods, BatchConsumer.start(), which
is invoked before the batch starts, and BatchConsumer.end(), which is invoked when
the
batch is submitted. The BatchConsumer will work for either single-operation mode or batch mode, but only a
BatchConsumer will be able to recognize the start and end of a batch.
| Constructor and Description |
|---|
OperationBatcher(reactor.fn.Supplier<T> dataSupplier,
reactor.fn.Consumer<T> consumer,
Registry<reactor.fn.Consumer<java.lang.Throwable>> errorConsumers,
reactor.jarjar.com.lmax.disruptor.WaitStrategy waitStrategy,
boolean multiThreadedProducer,
int opsBufferSize) |
| Modifier and Type | Method and Description |
|---|---|
OperationBatcher<T> |
batch(int size,
reactor.fn.Consumer<T> mutator)
If the
Consumer set in the spec is a BatchConsumer, then the start method will be invoked before
the batch is published, then all the events of the batch are published to the consumer, then the batch end is
published. |
OperationBatcher<T> |
commit(java.util.List<Operation<T>> ops)
Commit a list of
Operations by doing a batch publish. |
Operation<T> |
get() |
Operation<T> |
prepare()
Prepare an
Operation by allocating it from the buffer and preparing it to be submitted once Operation.commit() is invoked. |
void |
shutdown()
Shutdown this
Processor by shutting down the thread pool. |
public OperationBatcher(@Nonnull
reactor.fn.Supplier<T> dataSupplier,
@Nonnull
reactor.fn.Consumer<T> consumer,
@Nonnull
Registry<reactor.fn.Consumer<java.lang.Throwable>> errorConsumers,
reactor.jarjar.com.lmax.disruptor.WaitStrategy waitStrategy,
boolean multiThreadedProducer,
int opsBufferSize)
public void shutdown()
Processor by shutting down the thread pool.public Operation<T> prepare()
Operation by allocating it from the buffer and preparing it to be submitted once Operation.commit() is invoked.Operation instance allocated from the bufferpublic OperationBatcher<T> commit(java.util.List<Operation<T>> ops)
Operations by doing a batch publish.ops - the Operations to commitpublic OperationBatcher<T> batch(int size, reactor.fn.Consumer<T> mutator)
Consumer set in the spec is a BatchConsumer, then the start method will be invoked before
the batch is published, then all the events of the batch are published to the consumer, then the batch end is
published.
The Consumer passed here is a mutator. Rather than accessing the data object directly, as one would do
with a prepare() call, pass a Consumer here that will accept the allocated data object which one
can update with the appropriate data. Note that this is not an event handler. The event handler Consumer
is
specified in the spec (which is passed into the Processor constructor).
size - size of the batchmutator - a Consumer that mutates the data object before it is published as an event and handled by the
event Consumer