disruptor.start();
@Test(timeout = 1000) public void shouldShutdownGracefulEvenWithFatalExceptionHandler() { disruptor.start(); byte[] bytes; for (int i = 1; i < 10; i++) { bytes = new byte[32]; random.nextBytes(bytes); disruptor.publishEvent(new ByteArrayTranslator(bytes)); } }
private TestEvent publishEvent() throws InterruptedException, BrokenBarrierException { if (ringBuffer == null) { ringBuffer = disruptor.start(); for (DelayedEventHandler eventHandler : delayedEventHandlers) { eventHandler.awaitStart(); } } disruptor.publishEvent( new EventTranslator<TestEvent>() { @Override public void translateTo(final TestEvent event, final long sequence) { lastPublishedEvent = event; } }); return lastPublishedEvent; }
@Test public void shouldBatchOfEvents() throws Exception { final CountDownLatch eventCounter = new CountDownLatch(2); disruptor.handleEventsWith(new EventHandler<TestEvent>() { @Override public void onEvent(final TestEvent event, final long sequence, final boolean endOfBatch) throws Exception { eventCounter.countDown(); } }); disruptor.start(); disruptor.publishEvents( new EventTranslatorOneArg<TestEvent, Object>() { @Override public void translateTo(final TestEvent event, final long sequence, Object arg) { lastPublishedEvent = event; } }, new Object[] { "a", "b" } ); if (!eventCounter.await(5, TimeUnit.SECONDS)) { fail("Did not process event published before start was called. Missed events: " + eventCounter.getCount()); } }
@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))); }
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(); } }
disruptor.start();
Publisher[] publishers = initialise(new Publisher[publisherCount], ringBuffer, iterations, barrier, latch); disruptor.start();
disruptor.start();
disruptor.start();
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(); } }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionWhenAddingEventProcessorsAfterTheProducerBarrierHasBeenCreated() throws Exception { executor.ignoreExecutions(); disruptor.handleEventsWith(new SleepingEventHandler()); disruptor.start(); disruptor.handleEventsWith(new SleepingEventHandler()); }
public static void main(String[] args) throws InterruptedException { Disruptor<ObjectBox> disruptor = new Disruptor<ObjectBox>( ObjectBox.FACTORY, RING_SIZE, DaemonThreadFactory.INSTANCE, ProducerType.MULTI, new BlockingWaitStrategy()); disruptor.handleEventsWith(new Consumer()).then(new Consumer()); final RingBuffer<ObjectBox> ringBuffer = disruptor.getRingBuffer(); Publisher p = new Publisher(); IMessage message = new IMessage(); ITransportable transportable = new ITransportable(); String streamName = "com.lmax.wibble"; System.out.println("publishing " + RING_SIZE + " messages"); for (int i = 0; i < RING_SIZE; i++) { ringBuffer.publishEvent(p, message, transportable, streamName); Thread.sleep(10); } System.out.println("start disruptor"); disruptor.start(); System.out.println("continue publishing"); while (true) { ringBuffer.publishEvent(p, message, transportable, streamName); Thread.sleep(10); } } }
public static void main(String[] args) throws TimeoutException, InterruptedException { Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>( LongEvent.FACTORY, 16, DaemonThreadFactory.INSTANCE ); CountDownLatch shutdownLatch = new CountDownLatch(2); disruptor.handleEventsWith(new Handler(shutdownLatch)).then(new Handler(shutdownLatch)); disruptor.start(); long next = disruptor.getRingBuffer().next(); disruptor.getRingBuffer().get(next).set(next); disruptor.getRingBuffer().publish(next); disruptor.shutdown(10, TimeUnit.SECONDS); shutdownLatch.await(); System.out.println(value); } }
RingBuffer<LongEvent> buffer = d.start();
RingBuffer<StubEvent> ringBuffer = disruptor.start();
@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(); }
public static void main(String[] args) { Disruptor<PipelinerEvent> disruptor = new Disruptor<PipelinerEvent>( PipelinerEvent.FACTORY, 1024, DaemonThreadFactory.INSTANCE); disruptor.handleEventsWith( new ParallelHandler(0, 3), new ParallelHandler(1, 3), new ParallelHandler(2, 3) ).then(new JoiningHandler()); RingBuffer<PipelinerEvent> ringBuffer = disruptor.start(); for (int i = 0; i < 1000; i++) { long next = ringBuffer.next(); try { PipelinerEvent pipelinerEvent = ringBuffer.get(next); pipelinerEvent.input = i; } finally { ringBuffer.publish(next); } } }
@Test public void shouldBlockProducerUntilAllEventProcessorsHaveAdvanced() throws Exception { final DelayedEventHandler delayedEventHandler = createDelayedEventHandler(); disruptor.handleEventsWith(delayedEventHandler); final RingBuffer<TestEvent> ringBuffer = disruptor.start(); delayedEventHandler.awaitStart(); final StubPublisher stubPublisher = new StubPublisher(ringBuffer); try { executor.newThread(stubPublisher).start(); assertProducerReaches(stubPublisher, 4, true); delayedEventHandler.processEvent(); delayedEventHandler.processEvent(); delayedEventHandler.processEvent(); delayedEventHandler.processEvent(); delayedEventHandler.processEvent(); assertProducerReaches(stubPublisher, 5, false); } finally { stubPublisher.halt(); } }