use of com.lmax.disruptor.EventProcessor in project disruptor by LMAX-Exchange.
the class Disruptor method handleEventsWith.
/**
* <p>Set up custom event processors to handle events from the ring buffer. The Disruptor will
* automatically start this processors when {@link #start()} is called.</p>
* <p>
* <p>This method can be used as the start of a chain. For example if the processor <code>A</code> must
* process events before handler <code>B</code>:</p>
* <pre><code>dw.handleEventsWith(A).then(B);</code></pre>
*
* @param processors the event processors that will process events.
* @return a {@link EventHandlerGroup} that can be used to chain dependencies.
*/
public EventHandlerGroup<T> handleEventsWith(final EventProcessor... processors) {
for (final EventProcessor processor : processors) {
consumerRepository.add(processor);
}
Sequence[] sequences = new Sequence[processors.length];
for (int i = 0; i < processors.length; i++) {
sequences[i] = processors[i].getSequence();
}
ringBuffer.addGatingSequences(sequences);
return new EventHandlerGroup<T>(this, consumerRepository, Util.getSequencesFor(processors));
}
use of com.lmax.disruptor.EventProcessor in project disruptor by LMAX-Exchange.
the class DisruptorTest method shouldMakeEntriesAvailableToFirstCustomProcessorsImmediately.
@Test
public void shouldMakeEntriesAvailableToFirstCustomProcessorsImmediately() throws Exception {
final CountDownLatch countDownLatch = new CountDownLatch(2);
final EventHandler<TestEvent> eventHandler = new EventHandlerStub<TestEvent>(countDownLatch);
disruptor.handleEventsWith(new EventProcessorFactory<TestEvent>() {
@Override
public EventProcessor createEventProcessor(final RingBuffer<TestEvent> ringBuffer, final Sequence[] barrierSequences) {
assertEquals("Should not have had any barrier sequences", 0, barrierSequences.length);
return new BatchEventProcessor<TestEvent>(disruptor.getRingBuffer(), ringBuffer.newBarrier(barrierSequences), eventHandler);
}
});
ensureTwoEventsProcessedAccordingToDependencies(countDownLatch);
}
use of com.lmax.disruptor.EventProcessor in project disruptor by LMAX-Exchange.
the class DisruptorTest method shouldHonourDependenciesForCustomProcessors.
@Test
public void shouldHonourDependenciesForCustomProcessors() throws Exception {
final CountDownLatch countDownLatch = new CountDownLatch(2);
final EventHandler<TestEvent> eventHandler = new EventHandlerStub<TestEvent>(countDownLatch);
final DelayedEventHandler delayedEventHandler = createDelayedEventHandler();
disruptor.handleEventsWith(delayedEventHandler).then(new EventProcessorFactory<TestEvent>() {
@Override
public EventProcessor createEventProcessor(final RingBuffer<TestEvent> ringBuffer, final Sequence[] barrierSequences) {
assertSame("Should have had a barrier sequence", 1, barrierSequences.length);
return new BatchEventProcessor<TestEvent>(disruptor.getRingBuffer(), ringBuffer.newBarrier(barrierSequences), eventHandler);
}
});
ensureTwoEventsProcessedAccordingToDependencies(countDownLatch, delayedEventHandler);
}
Aggregations