Search in sources :

Example 1 with EventProcessor

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));
}
Also used : EventProcessor(com.lmax.disruptor.EventProcessor) BatchEventProcessor(com.lmax.disruptor.BatchEventProcessor) Sequence(com.lmax.disruptor.Sequence)

Example 2 with EventProcessor

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);
}
Also used : EventHandlerStub(com.lmax.disruptor.dsl.stubs.EventHandlerStub) TestEvent(com.lmax.disruptor.support.TestEvent) EventProcessor(com.lmax.disruptor.EventProcessor) BatchEventProcessor(com.lmax.disruptor.BatchEventProcessor) Sequence(com.lmax.disruptor.Sequence) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with EventProcessor

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);
}
Also used : EventHandlerStub(com.lmax.disruptor.dsl.stubs.EventHandlerStub) TestEvent(com.lmax.disruptor.support.TestEvent) EventProcessor(com.lmax.disruptor.EventProcessor) BatchEventProcessor(com.lmax.disruptor.BatchEventProcessor) Sequence(com.lmax.disruptor.Sequence) CountDownLatch(java.util.concurrent.CountDownLatch) DelayedEventHandler(com.lmax.disruptor.dsl.stubs.DelayedEventHandler) Test(org.junit.Test)

Aggregations

BatchEventProcessor (com.lmax.disruptor.BatchEventProcessor)3 EventProcessor (com.lmax.disruptor.EventProcessor)3 Sequence (com.lmax.disruptor.Sequence)3 EventHandlerStub (com.lmax.disruptor.dsl.stubs.EventHandlerStub)2 TestEvent (com.lmax.disruptor.support.TestEvent)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 DelayedEventHandler (com.lmax.disruptor.dsl.stubs.DelayedEventHandler)1