Search in sources :

Example 1 with BatchEventProcessor

use of com.lmax.disruptor.BatchEventProcessor in project disruptor by LMAX-Exchange.

the class DisruptorTest method shouldSupportCustomProcessorsAsDependencies.

@Test
public void shouldSupportCustomProcessorsAsDependencies() throws Exception {
    RingBuffer<TestEvent> ringBuffer = disruptor.getRingBuffer();
    final DelayedEventHandler delayedEventHandler = createDelayedEventHandler();
    CountDownLatch countDownLatch = new CountDownLatch(2);
    EventHandler<TestEvent> handlerWithBarrier = new EventHandlerStub<TestEvent>(countDownLatch);
    final BatchEventProcessor<TestEvent> processor = new BatchEventProcessor<TestEvent>(ringBuffer, ringBuffer.newBarrier(), delayedEventHandler);
    disruptor.handleEventsWith(processor).then(handlerWithBarrier);
    ensureTwoEventsProcessedAccordingToDependencies(countDownLatch, delayedEventHandler);
}
Also used : TestEvent(com.lmax.disruptor.support.TestEvent) EventHandlerStub(com.lmax.disruptor.dsl.stubs.EventHandlerStub) BatchEventProcessor(com.lmax.disruptor.BatchEventProcessor) CountDownLatch(java.util.concurrent.CountDownLatch) DelayedEventHandler(com.lmax.disruptor.dsl.stubs.DelayedEventHandler) Test(org.junit.Test)

Example 2 with BatchEventProcessor

use of com.lmax.disruptor.BatchEventProcessor 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 BatchEventProcessor

use of com.lmax.disruptor.BatchEventProcessor in project disruptor by LMAX-Exchange.

the class Disruptor method createEventProcessors.

EventHandlerGroup<T> createEventProcessors(final Sequence[] barrierSequences, final EventHandler<? super T>[] eventHandlers) {
    checkNotStarted();
    final Sequence[] processorSequences = new Sequence[eventHandlers.length];
    final SequenceBarrier barrier = ringBuffer.newBarrier(barrierSequences);
    for (int i = 0, eventHandlersLength = eventHandlers.length; i < eventHandlersLength; i++) {
        final EventHandler<? super T> eventHandler = eventHandlers[i];
        final BatchEventProcessor<T> batchEventProcessor = new BatchEventProcessor<T>(ringBuffer, barrier, eventHandler);
        if (exceptionHandler != null) {
            batchEventProcessor.setExceptionHandler(exceptionHandler);
        }
        consumerRepository.add(batchEventProcessor, eventHandler, barrier);
        processorSequences[i] = batchEventProcessor.getSequence();
    }
    updateGatingSequencesForNextInChain(barrierSequences, processorSequences);
    return new EventHandlerGroup<T>(this, consumerRepository, processorSequences);
}
Also used : SequenceBarrier(com.lmax.disruptor.SequenceBarrier) BatchEventProcessor(com.lmax.disruptor.BatchEventProcessor) Sequence(com.lmax.disruptor.Sequence)

Example 4 with BatchEventProcessor

use of com.lmax.disruptor.BatchEventProcessor in project disruptor by LMAX-Exchange.

the class DisruptorTest method shouldAddEventProcessorsAfterPublishing.

@Test
public void shouldAddEventProcessorsAfterPublishing() throws Exception {
    RingBuffer<TestEvent> rb = disruptor.getRingBuffer();
    BatchEventProcessor<TestEvent> b1 = new BatchEventProcessor<TestEvent>(rb, rb.newBarrier(), new SleepingEventHandler());
    BatchEventProcessor<TestEvent> b2 = new BatchEventProcessor<TestEvent>(rb, rb.newBarrier(b1.getSequence()), new SleepingEventHandler());
    BatchEventProcessor<TestEvent> b3 = new BatchEventProcessor<TestEvent>(rb, rb.newBarrier(b2.getSequence()), new SleepingEventHandler());
    assertThat(b1.getSequence().get(), is(-1L));
    assertThat(b2.getSequence().get(), is(-1L));
    assertThat(b3.getSequence().get(), is(-1L));
    rb.publish(rb.next());
    rb.publish(rb.next());
    rb.publish(rb.next());
    rb.publish(rb.next());
    rb.publish(rb.next());
    rb.publish(rb.next());
    disruptor.handleEventsWith(b1, b2, b3);
    assertThat(b1.getSequence().get(), is(5L));
    assertThat(b2.getSequence().get(), is(5L));
    assertThat(b3.getSequence().get(), is(5L));
}
Also used : SleepingEventHandler(com.lmax.disruptor.dsl.stubs.SleepingEventHandler) TestEvent(com.lmax.disruptor.support.TestEvent) BatchEventProcessor(com.lmax.disruptor.BatchEventProcessor) Test(org.junit.Test)

Example 5 with BatchEventProcessor

use of com.lmax.disruptor.BatchEventProcessor in project disruptor by LMAX-Exchange.

the class DisruptorTest method should.

@Test
public void should() throws Exception {
    RingBuffer<TestEvent> rb = disruptor.getRingBuffer();
    BatchEventProcessor<TestEvent> b1 = new BatchEventProcessor<TestEvent>(rb, rb.newBarrier(), new SleepingEventHandler());
    EventProcessorFactory<TestEvent> b2 = new EventProcessorFactory<TestEvent>() {

        @Override
        public EventProcessor createEventProcessor(RingBuffer<TestEvent> ringBuffer, Sequence[] barrierSequences) {
            return new BatchEventProcessor<TestEvent>(ringBuffer, ringBuffer.newBarrier(barrierSequences), new SleepingEventHandler());
        }
    };
    disruptor.handleEventsWith(b1).then(b2);
    disruptor.start();
}
Also used : SleepingEventHandler(com.lmax.disruptor.dsl.stubs.SleepingEventHandler) TestEvent(com.lmax.disruptor.support.TestEvent) BatchEventProcessor(com.lmax.disruptor.BatchEventProcessor) RingBuffer(com.lmax.disruptor.RingBuffer) Test(org.junit.Test)

Aggregations

BatchEventProcessor (com.lmax.disruptor.BatchEventProcessor)8 TestEvent (com.lmax.disruptor.support.TestEvent)7 Test (org.junit.Test)7 EventHandlerStub (com.lmax.disruptor.dsl.stubs.EventHandlerStub)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 DelayedEventHandler (com.lmax.disruptor.dsl.stubs.DelayedEventHandler)4 Sequence (com.lmax.disruptor.Sequence)3 SequenceBarrier (com.lmax.disruptor.SequenceBarrier)3 EventProcessor (com.lmax.disruptor.EventProcessor)2 SleepingEventHandler (com.lmax.disruptor.dsl.stubs.SleepingEventHandler)2 RingBuffer (com.lmax.disruptor.RingBuffer)1