com.lmax.disruptor.dsl
Class Disruptor<T>

java.lang.Object
  extended by com.lmax.disruptor.dsl.Disruptor<T>
Type Parameters:
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.

A simple example of setting up the disruptor with two event handlers that must process events in order:

 Disruptor disruptor = new Disruptor(MyEvent.FACTORY, 32, Executors.newCachedThreadPool());
 EventHandler handler1 = new EventHandler() { ... };
 EventHandler handler2 = new EventHandler() { ... };
 disruptor.handleEventsWith(handler1);
 disruptor.after(handler1).handleEventsWith(handler2);

 RingBuffer ringBuffer = disruptor.start();


Constructor Summary
Disruptor(EventFactory<T> eventFactory, java.util.concurrent.Executor executor, ClaimStrategy claimStrategy, WaitStrategy waitStrategy)
          Create a new Disruptor.
Disruptor(EventFactory<T> eventFactory, int ringBufferSize, java.util.concurrent.Executor executor)
          Create a new Disruptor.
 
Method Summary
 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.
 SequenceBarrier getBarrierFor(EventHandler<T> handler)
          Get the SequenceBarrier used by a specific handler.
 RingBuffer<T> getRingBuffer()
          The the RingBuffer used by this Disruptor.
 void halt()
          Calls EventProcessor.halt() on all of the event processors created via this disruptor.
 EventHandlerGroup<T> handleEventsWith(EventHandler<T>... handlers)
          Set up event handlers to handleEventException events from the ring buffer.
 EventHandlerGroup<T> handleEventsWith(EventProcessor... processors)
          Set up custom event processors to handleEventException events from the ring buffer.
 ExceptionHandlerSetting<?> handleExceptionsFor(EventHandler<T> eventHandler)
          Override the default exception handler for a specific handler.
 void handleExceptionsWith(ExceptionHandler exceptionHandler)
          Specify an exception handler to be used for any future event handlers.
 void publishEvent(EventTranslator<T> eventTranslator)
          Publish an event to the ring buffer.
 void shutdown()
          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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Disruptor

public Disruptor(EventFactory<T> eventFactory,
                 int ringBufferSize,
                 java.util.concurrent.Executor executor)
Create a new Disruptor.

Parameters:
eventFactory - the factory to create events in the ring buffer.
ringBufferSize - the size of the ring buffer.
executor - an Executor to execute event processors.

Disruptor

public Disruptor(EventFactory<T> eventFactory,
                 java.util.concurrent.Executor executor,
                 ClaimStrategy claimStrategy,
                 WaitStrategy waitStrategy)
Create a new Disruptor.

Parameters:
eventFactory - the factory to create events in the ring buffer.
executor - an Executor to execute event processors.
claimStrategy - the claim strategy to use for the ring buffer.
waitStrategy - the wait strategy to use for the ring buffer.
Method Detail

handleEventsWith

public EventHandlerGroup<T> handleEventsWith(EventHandler<T>... handlers)
Set up event handlers to handleEventException 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);

Parameters:
handlers - the event handlers that will process events.
Returns:
a EventHandlerGroup that can be used to chain dependencies.

handleEventsWith

public EventHandlerGroup<T> handleEventsWith(EventProcessor... processors)
Set up custom event processors to handleEventException events from the ring buffer. The Disruptor will automatically start this processors when start() is called.

Parameters:
processors - the event processors that will process events.
Returns:
a EventHandlerGroup that can be used to chain dependencies.

handleExceptionsWith

public void handleExceptionsWith(ExceptionHandler 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.

Parameters:
exceptionHandler - the exception handler to use for any future EventProcessor.

handleExceptionsFor

public ExceptionHandlerSetting<?> handleExceptionsFor(EventHandler<T> eventHandler)
Override the default exception handler for a specific handler.
disruptorWizard.handleExceptionsIn(eventHandler).with(exceptionHandler);

Parameters:
eventHandler - the event handler to set a different exception handler for.
Returns:
an ExceptionHandlerSetting dsl object - intended to be used by chaining the with method call.

after

public 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);

Parameters:
handlers - the event handlers, previously set up with handleEventsWith(com.lmax.disruptor.EventHandler[]), that will form the barrier for subsequent handlers or processors.
Returns:
an EventHandlerGroup that can be used to setup a dependency barrier over the specified event handlers.

after

public EventHandlerGroup<T> after(EventProcessor... processors)
Create a group of event processors to be used as a dependency.

Parameters:
processors - the event processors, previously set up with handleEventsWith(com.lmax.disruptor.EventProcessor...), that will form the barrier for subsequent handlers or processors.
Returns:
an EventHandlerGroup that can be used to setup a SequenceBarrier over hte specified event processors.
See Also:
after(com.lmax.disruptor.EventHandler[])

publishEvent

public void publishEvent(EventTranslator<T> eventTranslator)
Publish an event to the ring buffer.

Parameters:
eventTranslator - the translator that will load data into the event.

start

public 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.

Returns:
the configured ring buffer.

halt

public void halt()
Calls EventProcessor.halt() on all of the event processors created via this disruptor.


shutdown

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.


getRingBuffer

public RingBuffer<T> getRingBuffer()
The the RingBuffer used by this Disruptor. This is useful for creating custom event processors if the behaviour of BatchEventProcessor is not suitable.

Returns:
the ring buffer used by this Disruptor.

getBarrierFor

public SequenceBarrier getBarrierFor(EventHandler<T> handler)
Get the SequenceBarrier used by a specific handler. Note that the SequenceBarrier may be shared by multiple event handlers.

Parameters:
handler - the handler to get the barrier for.
Returns:
the SequenceBarrier used by handler.


Copyright © 2011 LMAX Ltd. All Rights Reserved.