T
- the type of event used.public class Disruptor<T>
extends java.lang.Object
A DSL-style API for setting up the disruptor pattern around a ring buffer (aka the Builder pattern).
A simple example of setting up the disruptor with two event handlers that must process events in order:
Disruptor<MyEvent> disruptor = new Disruptor<MyEvent>(MyEvent.FACTORY, 32, Executors.newCachedThreadPool());
EventHandler<MyEvent> handler1 = new EventHandler<MyEvent>() { ... };
EventHandler<MyEvent> handler2 = new EventHandler<MyEvent>() { ... };
disruptor.handleEventsWith(handler1);
disruptor.after(handler1).handleEventsWith(handler2);
RingBuffer ringBuffer = disruptor.start();
Constructor and Description |
---|
Disruptor(EventFactory<T> eventFactory,
int ringBufferSize,
java.util.concurrent.Executor executor)
Deprecated.
Use a
ThreadFactory instead of an Executor as a the ThreadFactory
is able to report errors when it is unable to construct a thread to run a producer. |
Disruptor(EventFactory<T> eventFactory,
int ringBufferSize,
java.util.concurrent.Executor executor,
ProducerType producerType,
WaitStrategy waitStrategy)
Deprecated.
Use a
ThreadFactory instead of an Executor as a the ThreadFactory
is able to report errors when it is unable to construct a thread to run a producer. |
Disruptor(EventFactory<T> eventFactory,
int ringBufferSize,
java.util.concurrent.ThreadFactory threadFactory)
Create a new Disruptor.
|
Disruptor(EventFactory<T> eventFactory,
int ringBufferSize,
java.util.concurrent.ThreadFactory threadFactory,
ProducerType producerType,
WaitStrategy waitStrategy)
Create a new Disruptor.
|
Modifier and Type | Method and Description |
---|---|
EventHandlerGroup<T> |
after(EventHandler<T>... handlers)
Create a group of event handlers to be used as a dependency.
|
EventHandlerGroup<T> |
after(EventProcessor... processors)
Create a group of event processors to be used as a dependency.
|
T |
get(long sequence)
Get the event for a given sequence in the RingBuffer.
|
SequenceBarrier |
getBarrierFor(EventHandler<T> handler)
Get the
SequenceBarrier used by a specific handler. |
long |
getBufferSize()
The capacity of the data structure to hold entries.
|
long |
getCursor()
Get the value of the cursor indicating the published sequence.
|
RingBuffer<T> |
getRingBuffer()
The
RingBuffer used by this Disruptor. |
long |
getSequenceValueFor(EventHandler<T> b1)
Gets the sequence value for the specified event handlers.
|
void |
halt()
Calls
EventProcessor.halt() on all of the event processors created via this disruptor. |
EventHandlerGroup<T> |
handleEventsWith(EventHandler<? super T>... handlers)
Set up event handlers to handle events from the ring buffer.
|
EventHandlerGroup<T> |
handleEventsWith(EventProcessor... processors)
Set up custom event processors to handle events from the ring buffer.
|
EventHandlerGroup<T> |
handleEventsWith(EventProcessorFactory<T>... eventProcessorFactories)
Set up custom event processors to handle events from the ring buffer.
|
EventHandlerGroup<T> |
handleEventsWithWorkerPool(WorkHandler<T>... workHandlers)
Set up a
WorkerPool to distribute an event to one of a pool of work handler threads. |
ExceptionHandlerSetting<T> |
handleExceptionsFor(EventHandler<T> eventHandler)
Override the default exception handler for a specific handler.
|
void |
handleExceptionsWith(ExceptionHandler<? super T> exceptionHandler)
Deprecated.
This method only applies to future event handlers. Use setDefaultExceptionHandler instead which applies to existing and new event handlers.
|
void |
publishEvent(EventTranslator<T> eventTranslator)
Publish an event to the ring buffer.
|
<A> void |
publishEvent(EventTranslatorOneArg<T,A> eventTranslator,
A arg)
Publish an event to the ring buffer.
|
<A,B,C> void |
publishEvent(EventTranslatorThreeArg<T,A,B,C> eventTranslator,
A arg0,
B arg1,
C arg2)
Publish an event to the ring buffer.
|
<A,B> void |
publishEvent(EventTranslatorTwoArg<T,A,B> eventTranslator,
A arg0,
B arg1)
Publish an event to the ring buffer.
|
<A> void |
publishEvents(EventTranslatorOneArg<T,A> eventTranslator,
A[] arg)
Publish a batch of events to the ring buffer.
|
void |
setDefaultExceptionHandler(ExceptionHandler<? super T> exceptionHandler)
Specify an exception handler to be used for event handlers and worker pools created by this Disruptor.
|
void |
shutdown()
Waits until all events currently in the disruptor have been processed by all event processors
and then halts the processors.
|
void |
shutdown(long timeout,
java.util.concurrent.TimeUnit timeUnit)
Waits until all events currently in the disruptor have been processed by all event processors
and then halts the processors.
|
RingBuffer<T> |
start()
Starts the event processors and returns the fully configured ring buffer.
|
java.lang.String |
toString() |
@Deprecated public Disruptor(EventFactory<T> eventFactory, int ringBufferSize, java.util.concurrent.Executor executor)
ThreadFactory
instead of an Executor
as a the ThreadFactory
is able to report errors when it is unable to construct a thread to run a producer.BlockingWaitStrategy
and
ProducerType
.MULTIeventFactory
- the factory to create events in the ring buffer.ringBufferSize
- the size of the ring buffer.executor
- an Executor
to execute event processors.@Deprecated public Disruptor(EventFactory<T> eventFactory, int ringBufferSize, java.util.concurrent.Executor executor, ProducerType producerType, WaitStrategy waitStrategy)
ThreadFactory
instead of an Executor
as a the ThreadFactory
is able to report errors when it is unable to construct a thread to run a producer.eventFactory
- the factory to create events in the ring buffer.ringBufferSize
- the size of the ring buffer, must be power of 2.executor
- an Executor
to execute event processors.producerType
- the claim strategy to use for the ring buffer.waitStrategy
- the wait strategy to use for the ring buffer.public Disruptor(EventFactory<T> eventFactory, int ringBufferSize, java.util.concurrent.ThreadFactory threadFactory)
BlockingWaitStrategy
and
ProducerType
.MULTIeventFactory
- the factory to create events in the ring buffer.ringBufferSize
- the size of the ring buffer.threadFactory
- a ThreadFactory
to create threads to for processors.public Disruptor(EventFactory<T> eventFactory, int ringBufferSize, java.util.concurrent.ThreadFactory threadFactory, ProducerType producerType, WaitStrategy waitStrategy)
eventFactory
- the factory to create events in the ring buffer.ringBufferSize
- the size of the ring buffer, must be power of 2.threadFactory
- a ThreadFactory
to create threads for processors.producerType
- the claim strategy to use for the ring buffer.waitStrategy
- the wait strategy to use for the ring buffer.@SafeVarargs public final EventHandlerGroup<T> handleEventsWith(EventHandler<? super T>... handlers)
Set up event handlers to handle events from the ring buffer. These handlers will process events as soon as they become available, in parallel.
This method can be used as the start of a chain. For example if the handler A
must
process events before handler B
:
dw.handleEventsWith(A).then(B);
This call is additive, but generally should only be called once when setting up the Disruptor instance
handlers
- the event handlers that will process events.EventHandlerGroup
that can be used to chain dependencies.@SafeVarargs public final EventHandlerGroup<T> handleEventsWith(EventProcessorFactory<T>... eventProcessorFactories)
Set up custom event processors to handle events from the ring buffer. The Disruptor will
automatically start these processors when start()
is called.
This method can be used as the start of a chain. For example if the handler A
must
process events before handler B
:
dw.handleEventsWith(A).then(B);
Since this is the start of the chain, the processor factories will always be passed an empty Sequence
array, so the factory isn't necessary in this case. This method is provided for consistency with
EventHandlerGroup.handleEventsWith(EventProcessorFactory...)
and EventHandlerGroup.then(EventProcessorFactory...)
which do have barrier sequences to provide.
This call is additive, but generally should only be called once when setting up the Disruptor instance
eventProcessorFactories
- the event processor factories to use to create the event processors that will process events.EventHandlerGroup
that can be used to chain dependencies.public EventHandlerGroup<T> handleEventsWith(EventProcessor... processors)
Set up custom event processors to handle events from the ring buffer. The Disruptor will
automatically start this processors when start()
is called.
This method can be used as the start of a chain. For example if the processor A
must
process events before handler B
:
dw.handleEventsWith(A).then(B);
processors
- the event processors that will process events.EventHandlerGroup
that can be used to chain dependencies.@SafeVarargs public final EventHandlerGroup<T> handleEventsWithWorkerPool(WorkHandler<T>... workHandlers)
WorkerPool
to distribute an event to one of a pool of work handler threads.
Each event will only be processed by one of the work handlers.
The Disruptor will automatically start this processors when start()
is called.workHandlers
- the work handlers that will process events.EventHandlerGroup
that can be used to chain dependencies.public void handleExceptionsWith(ExceptionHandler<? super T> exceptionHandler)
Specify an exception handler to be used for any future event handlers.
Note that only event handlers set up after calling this method will use the exception handler.
exceptionHandler
- the exception handler to use for any future EventProcessor
.public void setDefaultExceptionHandler(ExceptionHandler<? super T> exceptionHandler)
Specify an exception handler to be used for event handlers and worker pools created by this Disruptor.
The exception handler will be used by existing and future event handlers and worker pools created by this Disruptor instance.
exceptionHandler
- the exception handler to use.public ExceptionHandlerSetting<T> handleExceptionsFor(EventHandler<T> eventHandler)
disruptorWizard.handleExceptionsIn(eventHandler).with(exceptionHandler);
eventHandler
- the event handler to set a different exception handler for.@SafeVarargs public final EventHandlerGroup<T> after(EventHandler<T>... handlers)
Create a group of event handlers to be used as a dependency.
For example if the handler A
must process events before handler B
:
dw.after(A).handleEventsWith(B);
handlers
- the event handlers, previously set up with handleEventsWith(com.lmax.disruptor.EventHandler[])
,
that will form the barrier for subsequent handlers or processors.EventHandlerGroup
that can be used to setup a dependency barrier over the specified event handlers.public EventHandlerGroup<T> after(EventProcessor... processors)
processors
- the event processors, previously set up with handleEventsWith(com.lmax.disruptor.EventProcessor...)
,
that will form the barrier for subsequent handlers or processors.EventHandlerGroup
that can be used to setup a SequenceBarrier
over the specified event processors.after(com.lmax.disruptor.EventHandler[])
public void publishEvent(EventTranslator<T> eventTranslator)
eventTranslator
- the translator that will load data into the event.public <A> void publishEvent(EventTranslatorOneArg<T,A> eventTranslator, A arg)
A
- Class of the user supplied argument.eventTranslator
- the translator that will load data into the event.arg
- A single argument to load into the eventpublic <A> void publishEvents(EventTranslatorOneArg<T,A> eventTranslator, A[] arg)
A
- Class of the user supplied argument.eventTranslator
- the translator that will load data into the event.arg
- An array single arguments to load into the events. One Per event.public <A,B> void publishEvent(EventTranslatorTwoArg<T,A,B> eventTranslator, A arg0, B arg1)
A
- Class of the user supplied argument.B
- Class of the user supplied argument.eventTranslator
- the translator that will load data into the event.arg0
- The first argument to load into the eventarg1
- The second argument to load into the eventpublic <A,B,C> void publishEvent(EventTranslatorThreeArg<T,A,B,C> eventTranslator, A arg0, B arg1, C arg2)
A
- Class of the user supplied argument.B
- Class of the user supplied argument.C
- Class of the user supplied argument.eventTranslator
- the translator that will load data into the event.arg0
- The first argument to load into the eventarg1
- The second argument to load into the eventarg2
- The third argument to load into the eventpublic RingBuffer<T> start()
Starts the event processors and returns the fully configured ring buffer.
The ring buffer is set up to prevent overwriting any entry that is yet to be processed by the slowest event processor.
This method must only be called once after all event processors have been added.
public void halt()
EventProcessor.halt()
on all of the event processors created via this disruptor.public void shutdown()
Waits until all events currently in the disruptor have been processed by all event processors and then halts the processors. It is critical that publishing to the ring buffer has stopped before calling this method, otherwise it may never return.
This method will not shutdown the executor, nor will it await the final termination of the processor threads.
public void shutdown(long timeout, java.util.concurrent.TimeUnit timeUnit) throws TimeoutException
Waits until all events currently in the disruptor have been processed by all event processors and then halts the processors.
This method will not shutdown the executor, nor will it await the final termination of the processor threads.
timeout
- the amount of time to wait for all events to be processed. -1
will give an infinite timeouttimeUnit
- the unit the timeOut is specified inTimeoutException
- if a timeout occurs before shutdown completes.public RingBuffer<T> getRingBuffer()
RingBuffer
used by this Disruptor. This is useful for creating custom
event processors if the behaviour of BatchEventProcessor
is not suitable.public long getCursor()
public long getBufferSize()
Sequenced.getBufferSize()
public T get(long sequence)
sequence
- for the event.RingBuffer.get(long)
public SequenceBarrier getBarrierFor(EventHandler<T> handler)
SequenceBarrier
used by a specific handler. Note that the SequenceBarrier
may be shared by multiple event handlers.handler
- the handler to get the barrier for.public long getSequenceValueFor(EventHandler<T> b1)
b1
- eventHandler to get the sequence for.public java.lang.String toString()
toString
in class java.lang.Object
Copyright © 2011 - 2021 LMAX Ltd. All Rights Reserved.