EventHandlerGroup<T> createEventProcessors( final Sequence[] barrierSequences, final EventProcessorFactory<T>[] processorFactories) { final EventProcessor[] eventProcessors = new EventProcessor[processorFactories.length]; for (int i = 0; i < processorFactories.length; i++) { eventProcessors[i] = processorFactories[i].createEventProcessor(ringBuffer, barrierSequences); } return handleEventsWith(eventProcessors); }
@SuppressWarnings("unchecked") private TestEventHandler[] initialise(Disruptor<TestEvent> disruptor, TestEventHandler[] testEventHandlers) { for (int i = 0; i < testEventHandlers.length; i++) { TestEventHandler handler = new TestEventHandler(); disruptor.handleEventsWith(handler); testEventHandlers[i] = handler; } return testEventHandlers; }
@Test public void shouldAllowEventHandlerWithSuperType() throws Exception { final CountDownLatch latch = new CountDownLatch(2); final EventHandler<Object> objectHandler = new EventHandlerStub<Object>(latch); disruptor.handleEventsWith(objectHandler); ensureTwoEventsProcessedAccordingToDependencies(latch); }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionWhenAddingEventProcessorsAfterTheProducerBarrierHasBeenCreated() throws Exception { executor.ignoreExecutions(); disruptor.handleEventsWith(new SleepingEventHandler()); disruptor.start(); disruptor.handleEventsWith(new SleepingEventHandler()); }
@Test public void shouldMakeEntriesAvailableToFirstHandlersImmediately() throws Exception { CountDownLatch countDownLatch = new CountDownLatch(2); EventHandler<TestEvent> eventHandler = new EventHandlerStub<TestEvent>(countDownLatch); disruptor.handleEventsWith(createDelayedEventHandler(), eventHandler); ensureTwoEventsProcessedAccordingToDependencies(countDownLatch); }
@Test public void shouldWaitOnAllProducersJoinedByAnd() throws Exception { DelayedEventHandler handler1 = createDelayedEventHandler(); DelayedEventHandler handler2 = createDelayedEventHandler(); CountDownLatch countDownLatch = new CountDownLatch(2); EventHandler<TestEvent> handlerWithBarrier = new EventHandlerStub<TestEvent>(countDownLatch); disruptor.handleEventsWith(handler1); final EventHandlerGroup<TestEvent> handler2Group = disruptor.handleEventsWith(handler2); disruptor.after(handler1).and(handler2Group).handleEventsWith(handlerWithBarrier); ensureTwoEventsProcessedAccordingToDependencies(countDownLatch, handler1, handler2); }
@Test public void shouldWaitUntilAllFirstEventProcessorsProcessEventBeforeMakingItAvailableToDependentEventProcessors() throws Exception { DelayedEventHandler eventHandler1 = createDelayedEventHandler(); CountDownLatch countDownLatch = new CountDownLatch(2); EventHandler<TestEvent> eventHandler2 = new EventHandlerStub<TestEvent>(countDownLatch); disruptor.handleEventsWith(eventHandler1).then(eventHandler2); ensureTwoEventsProcessedAccordingToDependencies(countDownLatch, eventHandler1); }
@Test public void shouldAllowChainingEventHandlersWithSuperType() throws Exception { final CountDownLatch latch = new CountDownLatch(2); final DelayedEventHandler delayedEventHandler = createDelayedEventHandler(); final EventHandler<Object> objectHandler = new EventHandlerStub<Object>(latch); disruptor.handleEventsWith(delayedEventHandler).then(objectHandler); ensureTwoEventsProcessedAccordingToDependencies(latch, delayedEventHandler); }
@Test public void shouldCreateEventProcessorGroupForFirstEventProcessors() throws Exception { executor.ignoreExecutions(); final EventHandler<TestEvent> eventHandler1 = new SleepingEventHandler(); EventHandler<TestEvent> eventHandler2 = new SleepingEventHandler(); final EventHandlerGroup<TestEvent> eventHandlerGroup = disruptor.handleEventsWith(eventHandler1, eventHandler2); disruptor.start(); assertNotNull(eventHandlerGroup); assertThat(Integer.valueOf(executor.getExecutionCount()), equalTo(Integer.valueOf(2))); }
@Test(expected = TimeoutException.class, timeout = 2000) public void shouldThrowTimeoutExceptionIfShutdownDoesNotCompleteNormally() throws Exception { //Given final DelayedEventHandler delayedEventHandler = createDelayedEventHandler(); disruptor.handleEventsWith(delayedEventHandler); publishEvent(); //When disruptor.shutdown(1, SECONDS); //Then }
public static void main(String[] args) { Disruptor<MyEvent> disruptor = new Disruptor<MyEvent>(factory, 1024, DaemonThreadFactory.INSTANCE); disruptor.handleEventsWith(handler1).then(handler2).then(handler3); disruptor.start(); } }
@Test(expected = IllegalArgumentException.class) public void shouldTrackEventHandlersByIdentityNotEquality() throws Exception { EvilEqualsEventHandler handler1 = new EvilEqualsEventHandler(); EvilEqualsEventHandler handler2 = new EvilEqualsEventHandler(); disruptor.handleEventsWith(handler1); // handler2.equals(handler1) but it hasn't yet been registered so should throw exception. disruptor.after(handler2); }
@Test public void shouldBeAbleToOverrideTheExceptionHandlerForAEventProcessor() throws Exception { final RuntimeException testException = new RuntimeException(); final ExceptionThrowingEventHandler eventHandler = new ExceptionThrowingEventHandler(testException); disruptor.handleEventsWith(eventHandler); AtomicReference<Throwable> reference = new AtomicReference<Throwable>(); StubExceptionHandler exceptionHandler = new StubExceptionHandler(reference); disruptor.handleExceptionsFor(eventHandler).with(exceptionHandler); publishEvent(); waitFor(reference); }
@Test public void shouldSupportSpecifyingADefaultExceptionHandlerForEventProcessors() throws Exception { AtomicReference<Throwable> eventHandled = new AtomicReference<Throwable>(); ExceptionHandler exceptionHandler = new StubExceptionHandler(eventHandled); RuntimeException testException = new RuntimeException(); ExceptionThrowingEventHandler handler = new ExceptionThrowingEventHandler(testException); disruptor.setDefaultExceptionHandler(exceptionHandler); disruptor.handleEventsWith(handler); publishEvent(); final Throwable actualException = waitFor(eventHandled); assertSame(testException, actualException); }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionIfStartIsCalledTwice() throws Exception { executor.ignoreExecutions(); disruptor.handleEventsWith(new SleepingEventHandler()); disruptor.start(); disruptor.start(); }
@Test public void shouldAllowSpecifyingSpecificEventProcessorsToWaitFor() throws Exception { DelayedEventHandler handler1 = createDelayedEventHandler(); DelayedEventHandler handler2 = createDelayedEventHandler(); CountDownLatch countDownLatch = new CountDownLatch(2); EventHandler<TestEvent> handlerWithBarrier = new EventHandlerStub<TestEvent>(countDownLatch); disruptor.handleEventsWith(handler1, handler2); disruptor.after(handler1, handler2).handleEventsWith(handlerWithBarrier); ensureTwoEventsProcessedAccordingToDependencies(countDownLatch, handler1, handler2); }
public static void main(String[] args) { Executor executor = Executors.newFixedThreadPool(4); Disruptor<DataEvent> disruptor = new Disruptor<DataEvent>( DataEvent.FACTORY, 1024, DaemonThreadFactory.INSTANCE); TransformingHandler handler1 = new TransformingHandler(0); TransformingHandler handler2 = new TransformingHandler(1); TransformingHandler handler3 = new TransformingHandler(2); CollatingHandler collator = new CollatingHandler(); disruptor.handleEventsWith(handler1, handler2, handler3).then(collator); disruptor.start(); } }
public static void main(String[] args) { Disruptor<Event> disruptor = new Disruptor<>(Event.FACTORY, 1024, new DefaultThreadFactory()); AtomicBoolean running = new AtomicBoolean(true); ErrorHandler errorHandler = new ErrorHandler(running); final Handler handler = new Handler(); disruptor.handleEventsWith(handler); disruptor.handleExceptionsFor(handler).with(errorHandler); simplePublish(disruptor, running); }
@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); }
@SuppressWarnings("unchecked") @Before public void setUp() { disruptor = new Disruptor<byte[]>( new ByteArrayFactory(256), 1024, DaemonThreadFactory.INSTANCE, ProducerType.SINGLE, new BlockingWaitStrategy()); disruptor.handleEventsWith(eventHandler); disruptor.setDefaultExceptionHandler(new FatalExceptionHandler()); }