com.lmax.disruptor.EventHandler Java Examples

The following examples show how to use com.lmax.disruptor.EventHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DisruptorTest.java    From jstorm with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleProducer() throws InterruptedException {
    System.out.println("!!!!!!!!!!!!!!Begin testSingleProducer!!!!!!!!!!!!!!");
    final AtomicBoolean messageConsumed = new AtomicBoolean(false);

    // Set queue length to 1, so that the RingBuffer can be easily full
    // to trigger consumer blocking
    DisruptorQueue queue = createQueue("consumerHang", ProducerType.SINGLE, 1);
    push(queue, 1);
    Runnable producer = new Producer(queue);
    Runnable consumer = new Consumer(queue, new EventHandler<Object>() {
        long count = 0;

        @Override
        public void onEvent(Object obj, long sequence, boolean endOfBatch) throws Exception {

            messageConsumed.set(true);
            System.out.println("Consume " + count++);
        }
    });

    run(producer, 0, 0, consumer, 50);
    Assert.assertTrue("disruptor message is never consumed due to consumer thread hangs", messageConsumed.get());

    System.out.println("!!!!!!!!!!!!!!End testSingleProducer!!!!!!!!!!!!!!");
}
 
Example #2
Source File: Source.java    From cep with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Create a new source.
 * <p>This method will prepare the instance with some needed variables
 * in order to be started later with the start method (implemented by children).
 *
 * @param parsersManager Instance of ParserManager that will serve parsers to this source instance
 * @param eventHandler Instance of EventHandler that will receive the events generated by this source instance
 * @param properties Map of properties associated with this source
 */

public Source(ParsersManager parsersManager, EventHandler eventHandler, Map<String, Object> properties) {
    // Save the references for later use
    this.parsersManager = parsersManager;
    this.properties = properties;

    // Create the ring buffer for this topic and start it
    Disruptor<MapEvent> disruptor = new Disruptor<>(new MapEventFactory(), ConfigData.getRingBufferSize(), Executors.newCachedThreadPool());
    disruptor.handleEventsWith(eventHandler);
    disruptor.start();

    // Create the event producer that will receive the events produced by
    // this source instance
    eventProducer = new EventProducer(disruptor.getRingBuffer());
    prepare();
}
 
Example #3
Source File: AbstractGenericHandlerTest.java    From couchbase-jvm-core with Apache License 2.0 6 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setup() {
    responseBuffer = new Disruptor<ResponseEvent>(new EventFactory<ResponseEvent>() {
        @Override
        public ResponseEvent newInstance() {
            return new ResponseEvent();
        }
    }, 1024, Executors.newCachedThreadPool());

    firedEvents = Collections.synchronizedList(new ArrayList<CouchbaseMessage>());
    latch = new CountDownLatch(1);
    responseBuffer.handleEventsWith(new EventHandler<ResponseEvent>() {
        @Override
        public void onEvent(ResponseEvent event, long sequence, boolean endOfBatch) throws Exception {
            firedEvents.add(event.getMessage());
            latch.countDown();
        }
    });
    responseRingBuffer = responseBuffer.start();
}
 
Example #4
Source File: DefaultDisruptorConfigTest.java    From disruptor-spring-manager with MIT License 6 votes vote down vote up
/** 
 *                                            Consumer B1 -> Consumer C1
 *                                           /                          \
 * Publisher -> Ring buffer ---> Consumer A -                            -> Consumer D 
 *                                           \                          /
 *                                            Consumer B2 -> Consumer C2
 * 
 * Look at the graph that gets printed by log4j.
 */
@Test
public void test_publish_complicated_diamond_eventprocessor_topology() {
	ConsumerA consumerA = new ConsumerA();
	ConsumerB1 consumerB1 = new ConsumerB1();
	ConsumerB2 consumerB2 = new ConsumerB2();
	ConsumerC1 consumerC1 = new ConsumerC1();
	ConsumerC2 consumerC2 = new ConsumerC2();
	ConsumerD consumerD = new ConsumerD();
	
	EventHandlerChain<String> eventHandlerChain1 = new EventHandlerChain<String>(new EventHandler[]{consumerA}, new EventHandler[]{consumerB1, consumerB2});
	EventHandlerChain<String> eventHandlerChain2 = new EventHandlerChain<String>(new EventHandler[]{consumerB1}, new EventHandler[]{consumerC1});
	EventHandlerChain<String> eventHandlerChain3 = new EventHandlerChain<String>(new EventHandler[]{consumerB2}, new EventHandler[]{consumerC2});
	EventHandlerChain<String> eventHandlerChain4 = new EventHandlerChain<String>(new EventHandler[]{consumerC1, consumerC2}, new EventHandler[]{consumerD});
	
	disruptorConfig.setEventHandlerChain(new EventHandlerChain[]{eventHandlerChain1, eventHandlerChain2, eventHandlerChain3, eventHandlerChain4});
	disruptorConfig.init();
	
	disruptorConfig.publish(new EventTranslator<String>() {

		@Override
		public void translateTo(String event, long sequence) {
			event = "hi there";
		}
	});
}
 
Example #5
Source File: DefaultDisruptorConfigTest.java    From disruptor-spring-manager with MIT License 6 votes vote down vote up
/** 
 *                                            Consumer B1  
 *                                           /           \
 * Publisher -> Ring buffer ---> Consumer A -             -> Consumer D 
 *                                           \           /
 *                                            Consumer B2
 * 
 * Look at the graph that gets printed by log4j.
 */
@Test
public void test_publish_diamond_eventprocessor_topology() {
	ConsumerA consumerA = new ConsumerA();
	ConsumerB1 consumerB1 = new ConsumerB1();
	ConsumerB2 consumerB2 = new ConsumerB2();
	ConsumerD consumerD = new ConsumerD();
	
	EventHandlerChain<String> eventHandlerChain1 = new EventHandlerChain<String>(new EventHandler[]{consumerA}, new EventHandler[]{consumerB1, consumerB2});
	EventHandlerChain<String> eventHandlerChain2 = new EventHandlerChain<String>(new EventHandler[]{consumerB1, consumerB2}, new EventHandler[]{consumerD});
	
	disruptorConfig.setEventHandlerChain(new EventHandlerChain[]{eventHandlerChain1, eventHandlerChain2});
	disruptorConfig.init();
	
	disruptorConfig.publish(new EventTranslator<String>() {

		@Override
		public void translateTo(String event, long sequence) {
			event = "hi there";
		}
	});
}
 
Example #6
Source File: DefaultDisruptorConfigTest.java    From disruptor-spring-manager with MIT License 6 votes vote down vote up
/**
 * Publisher -> Ring buffer ---> Consumer A -> Consumer B1 -> Consumer D 
 * Look at the graph that gets printed by log4j.
 */
@Test
public void test_publish_simple_eventprocessor_topology() {
	ConsumerA consumerA = new ConsumerA();
	ConsumerB1 consumerB1 = new ConsumerB1();
	ConsumerD consumerD = new ConsumerD();
	
	EventHandlerChain<String> eventHandlerChain1 = new EventHandlerChain<String>(new EventHandler[]{consumerA}, new EventHandler[]{consumerB1});
	EventHandlerChain<String> eventHandlerChain2 = new EventHandlerChain<String>(new EventHandler[]{consumerB1}, new EventHandler[]{consumerD});
	
	disruptorConfig.setEventHandlerChain(new EventHandlerChain[]{eventHandlerChain1, eventHandlerChain2});
	disruptorConfig.init();
	
	disruptorConfig.publish(new EventTranslator<String>() {

		@Override
		public void translateTo(String event, long sequence) {
			event = "hi there";
		}
	});
}
 
Example #7
Source File: DefaultDisruptorConfigTest.java    From disruptor-spring-manager with MIT License 6 votes vote down vote up
/**
 * Publisher -> Ring buffer ---> Consumer A 
 * Look at the graph that gets printed by log4j.
 */
@Test
public void test_publish_single_eventprocessor_topology() {
	ConsumerA consumerA = new ConsumerA();
	
	EventHandlerChain<String> eventHandlerChain1 = new EventHandlerChain<String>(new EventHandler[]{consumerA});
	
	disruptorConfig.setEventHandlerChain(new EventHandlerChain[]{eventHandlerChain1});
	disruptorConfig.init();
	
	disruptorConfig.publish(new EventTranslator<String>() {

		@Override
		public void translateTo(String event, long sequence) {
			event = "hi there";
		}
	});
}
 
Example #8
Source File: DefaultRheaKVStore.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public Batching(EventFactory<T> factory, int bufSize, String name, EventHandler<T> handler) {
    this.name = name;
    this.disruptor = new Disruptor<>(factory, bufSize, new NamedThreadFactory(name, true));
    this.disruptor.handleEventsWith(handler);
    this.disruptor.setDefaultExceptionHandler(new LogExceptionHandler<Object>(name));
    this.ringBuffer = this.disruptor.start();
}
 
Example #9
Source File: DisruptorTest.java    From jstorm with Apache License 2.0 5 votes vote down vote up
@Test
public void testBeforeStartConsumer() throws InterruptedException {
    System.out.println("!!!!!!!!!!!!Begin testBeforeStartConsumer!!!!!!!!!");
    final AtomicBoolean messageConsumed = new AtomicBoolean(false);

    // Set queue length to 1, so that the RingBuffer can be easily full
    // to trigger consumer blocking
    DisruptorQueue queue = createQueue("consumerHang", ProducerType.MULTI, 2);
    //queue.consumerStarted();
    push(queue, 1);
    Runnable producer = new Producer(queue);
    Runnable consumer = new Consumer(queue, new EventHandler<Object>() {
        long count = 0;

        @Override
        public void onEvent(Object obj, long sequence, boolean endOfBatch) throws Exception {

            messageConsumed.set(true);
            System.out.println("Consume " + count++);
        }
    });

    run(producer, 0, 0, consumer, 50);
    Assert.assertTrue("disruptor message is never consumed due to consumer thread hangs", messageConsumed.get());

    System.out.println("!!!!!!!!!!!!!End testBeforeStartConsumer!!!!!!!!!!");
}
 
Example #10
Source File: DisruptorQueueImpl.java    From jstorm with Apache License 2.0 5 votes vote down vote up
public void asyncConsumeBatchToCursor(EventHandler<Object> handler) throws AlertException, InterruptedException, TimeoutException {
    List<Object> batch = getConsumeBatch();
    if (batch == null)
        return;

    for (int i = 0; i < batch.size(); i++) {
        try {
            handler.onEvent(batch.get(i), 0, i == (batch.size() - 1));
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }
}
 
Example #11
Source File: BaseExecutors.java    From jstorm with Apache License 2.0 5 votes vote down vote up
protected void consumeBatch(EventHandler<Object> handler) {
    boolean isConsumeEvent = false;
    if (controlQueue.population() > 0) {
        controlQueue.consumeBatch(handler);
        isConsumeEvent = true;
    }
    if (exeQueue.population() > 0) {
        exeQueue.consumeBatch(handler);
        isConsumeEvent = true;
    }
    if (!isConsumeEvent)
        JStormUtils.sleepMs(1);
}
 
Example #12
Source File: MultiEventConsumer.java    From tutorials with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public EventHandler<ValueEvent>[] getEventHandler() {
    final EventHandler<ValueEvent> eventHandler = (event, sequence, endOfBatch) -> assertExpectedValue(event.getValue());
    final EventHandler<ValueEvent> otherEventHandler = (event, sequence, endOfBatch) -> assertOtherExpectedValue(event.getValue());
    return new EventHandler[] { eventHandler, otherEventHandler };
}
 
Example #13
Source File: MultiEventPrintConsumer.java    From tutorials with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public EventHandler<ValueEvent>[] getEventHandler() {
    final EventHandler<ValueEvent> eventHandler = (event, sequence, endOfBatch) -> print(event.getValue(), sequence);
    final EventHandler<ValueEvent> otherEventHandler = (event, sequence, endOfBatch) -> print(event.getValue(), sequence);
    return new EventHandler[] { eventHandler, otherEventHandler };
}
 
Example #14
Source File: SearchHandlerTest.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setup() {

    responseBuffer = new Disruptor<ResponseEvent>(new EventFactory<ResponseEvent>() {
        @Override
        public ResponseEvent newInstance() {
            return new ResponseEvent();
        }
    }, 1024, Executors.newCachedThreadPool());

    firedEvents = Collections.synchronizedList(new ArrayList<CouchbaseMessage>());
    latch = new CountDownLatch(1);
    responseBuffer.handleEventsWith(new EventHandler<ResponseEvent>() {
        @Override
        public void onEvent(ResponseEvent event, long sequence, boolean endOfBatch) throws Exception {
            firedEvents.add(event.getMessage());
            latch.countDown();
        }
    });
    responseRingBuffer = responseBuffer.start();

    CoreEnvironment environment = mock(CoreEnvironment.class);
    when(environment.scheduler()).thenReturn(Schedulers.computation());
    when(environment.maxRequestLifetime()).thenReturn(10000L); // 10 seconds
    when(environment.autoreleaseAfter()).thenReturn(2000L);
    when(environment.retryStrategy()).thenReturn(FailFastRetryStrategy.INSTANCE);
    endpoint = mock(AbstractEndpoint.class);
    when(endpoint.environment()).thenReturn(environment);
    when(environment.userAgent()).thenReturn("Couchbase Client Mock");

    queue = new ArrayDeque<SearchRequest>();
    handler = new SearchHandler(endpoint, responseRingBuffer, queue, false, false);
    channel = new EmbeddedChannel(handler);
}
 
Example #15
Source File: QueryHandlerTest.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
protected void commonSetup() {
    responseBuffer = new Disruptor<ResponseEvent>(new EventFactory<ResponseEvent>() {
        @Override
        public ResponseEvent newInstance() {
            return new ResponseEvent();
        }
    }, 1024, Executors.newCachedThreadPool());

    firedEvents = Collections.synchronizedList(new ArrayList<CouchbaseMessage>());
    latch = new CountDownLatch(1);
    responseBuffer.handleEventsWith(new EventHandler<ResponseEvent>() {
        @Override
        public void onEvent(ResponseEvent event, long sequence, boolean endOfBatch) throws Exception {
            firedEvents.add(event.getMessage());
            latch.countDown();
        }
    });
    responseRingBuffer = responseBuffer.start();

    CoreEnvironment environment = mock(CoreEnvironment.class);
    when(environment.scheduler()).thenReturn(Schedulers.computation());
    when(environment.maxRequestLifetime()).thenReturn(10000L);
    when(environment.autoreleaseAfter()).thenReturn(2000L);
    when(environment.retryStrategy()).thenReturn(FailFastRetryStrategy.INSTANCE);
    endpoint = mock(AbstractEndpoint.class);
    when(endpoint.environment()).thenReturn(environment);
    when(endpoint.context()).thenReturn(new CoreContext(environment, null, 1));
    when(environment.userAgent()).thenReturn("Couchbase Client Mock");

    queue = new ArrayDeque<QueryRequest>();
}
 
Example #16
Source File: ViewHandlerTest.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Before
@SuppressWarnings("unchecked")
public void setup() {
    responseBuffer = new Disruptor<ResponseEvent>(new EventFactory<ResponseEvent>() {
        @Override
        public ResponseEvent newInstance() {
            return new ResponseEvent();
        }
    }, 1024, Executors.newCachedThreadPool());

    firedEvents = Collections.synchronizedList(new ArrayList<CouchbaseMessage>());
    latch = new CountDownLatch(1);
    responseBuffer.handleEventsWith(new EventHandler<ResponseEvent>() {
        @Override
        public void onEvent(ResponseEvent event, long sequence, boolean endOfBatch) throws Exception {
            firedEvents.add(event.getMessage());
            latch.countDown();
        }
    });
    responseRingBuffer = responseBuffer.start();

    CoreEnvironment environment = mock(CoreEnvironment.class);
    when(environment.scheduler()).thenReturn(Schedulers.computation());
    when(environment.maxRequestLifetime()).thenReturn(10000L); // 10 seconds
    when(environment.autoreleaseAfter()).thenReturn(2000L);
    when(environment.retryStrategy()).thenReturn(FailFastRetryStrategy.INSTANCE);
    endpoint = mock(AbstractEndpoint.class);
    when(endpoint.environment()).thenReturn(environment);
    when(endpoint.context()).thenReturn(new CoreContext(environment, null, 1));
    when(environment.userAgent()).thenReturn("Couchbase Client Mock");

    queue = new ArrayDeque<ViewRequest>();
    handler = new ViewHandler(endpoint, responseRingBuffer, queue, false, false);
    channel = new EmbeddedChannel(handler);
}
 
Example #17
Source File: MessageThread.java    From litchi with Apache License 2.0 5 votes vote down vote up
public MessageThread(String threadName, int bufferSize, EventHandler<MessageBuffer> handler, WaitStrategy waitStrategy) {
    this.disruptor = new Disruptor<>(
            () -> new MessageBuffer(),
            bufferSize,
            new NamedThreadFactory(threadName),
            ProducerType.MULTI,
            waitStrategy
    );

    disruptor.handleEventsWith(handler);
    disruptor.setDefaultExceptionHandler(new ErrorHandler<>());
}
 
Example #18
Source File: LogicProcessor.java    From Okra with Apache License 2.0 5 votes vote down vote up
public LogicProcessor(EventFactory<ConcurrentEvent> factory, EventHandler<ConcurrentEvent> handler, int rbSize, ExecutorService es, ProducerType pt, WaitStrategy ws) {
    this.disruptor = new Disruptor<>(factory, rbSize, es, pt, ws);
    this.disruptor.handleEventsWith(handler);
    //  disruptor.handleExceptionsWith();
    this.disruptor.start();
    this.msgQueue = newQueue();
}
 
Example #19
Source File: EventHandlerAdapter.java    From perf-workshop with Apache License 2.0 4 votes vote down vote up
public static EventHandler<Packet> wrap(final Consumer<Packet> consumer)
{
    return new EventHandlerAdapter(consumer);
}
 
Example #20
Source File: DisruptorTest.java    From jstorm with Apache License 2.0 4 votes vote down vote up
Consumer(DisruptorQueue queue, EventHandler handler) {
    this.handler = handler;
    this.queue = queue;
}
 
Example #21
Source File: DisruptorTest.java    From jstorm with Apache License 2.0 4 votes vote down vote up
@Test
public void testMessageDisorder() throws InterruptedException {

    System.out.println("!!!!!!!!!!!!!!!!Begin testMessageDisorder!!!!!!!!!!");
    // Set queue length to bigger enough
    DisruptorQueue queue = createQueue("messageOrder", ProducerType.MULTI, 128);

    queue.publish("1");

    Runnable producer = new Producer(queue);

    final Object[] result = new Object[1];
    Runnable consumer = new Consumer(queue, new EventHandler<Object>() {
        private boolean head = true;
        private Map<String, Long> lastIdMap = new HashMap<String, Long>();

        @Override
        public void onEvent(Object obj, long sequence, boolean endOfBatch) throws Exception {
            consumerNum.incrementAndGet();
            if (head) {
                head = false;
                result[0] = obj;
            } else {
                String event = (String) obj;
                String[] item = event.split("@");
                Long current = Long.valueOf(item[1]);
                Long last = lastIdMap.get(item[0]);
                if (last != null) {
                    if (current <= last) {
                        String msg = "Consume disorder of " + item[0] + ", current" + current + ",last:" + last;
                        System.err.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                        System.err.println(msg);
                        System.err.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                        Assert.fail(msg);
                    }
                }

                lastIdMap.put(item[0], current);

            }
        }
    });

    run(producer, PRODUCER_NUM, 1000, consumer, 30000);
    Assert.assertEquals("We expect to receive first published message first, but received " + result[0], "1", result[0]);
    produceNum.incrementAndGet();
    Assert.assertEquals("produce: " + produceNum.get() + ", consume:" + consumerNum.get(), produceNum.get(), consumerNum.get());
    System.out.println("!!!!!!!!!!!!!!End testMessageDisorder!!!!!!!!!!!!");
}
 
Example #22
Source File: DefaultRheaKVStore.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
public GetBatching(EventFactory<KeyEvent> factory, String name, EventHandler<KeyEvent> handler) {
    super(factory, batchingOpts.getBufSize(), name, handler);
}
 
Example #23
Source File: ExchangeCore.java    From exchange-core with Apache License 2.0 4 votes vote down vote up
private static EventHandler<OrderCommand>[] arraysAddHandler(EventHandler<OrderCommand>[] handlers, EventHandler<OrderCommand> extraHandler) {
    final EventHandler<OrderCommand>[] result = Arrays.copyOf(handlers, handlers.length + 1);
    result[handlers.length] = extraHandler;
    return result;
}
 
Example #24
Source File: DisruptorQueueImpl.java    From jstorm with Apache License 2.0 4 votes vote down vote up
public void multiConsumeBatchWhenAvailableWithCallback(EventHandler<Object> handler) {
    consumeBatchWhenAvailable(handler, false);
    handlerCallback();
}
 
Example #25
Source File: DisruptorQueueImpl.java    From jstorm with Apache License 2.0 4 votes vote down vote up
public void multiConsumeBatchWhenAvailable(EventHandler<Object> handler) {
    consumeBatchWhenAvailable(handler, false);
}
 
Example #26
Source File: DisruptorQueueImpl.java    From jstorm with Apache License 2.0 4 votes vote down vote up
public void consumeBatchWhenAvailableWithCallback(EventHandler<Object> handler) {
    consumeBatchWhenAvailable(handler);
    handlerCallback();
}
 
Example #27
Source File: DisruptorQueueImpl.java    From jstorm with Apache License 2.0 4 votes vote down vote up
public void consumeBatchWhenAvailable(EventHandler<Object> handler) {
    consumeBatchWhenAvailable(handler, true);
}
 
Example #28
Source File: DisruptorQueueImpl.java    From jstorm with Apache License 2.0 4 votes vote down vote up
public void consumeBatch(EventHandler<Object> handler) {
    // write pos > read pos
    // Asynchronous release the queue, but still is single thread
    if (_buffer.getCursor() > _consumer.get())
        consumeBatchWhenAvailable(handler);
}
 
Example #29
Source File: ExchangeCore.java    From exchange-core with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings(value = {"unchecked"})
private static EventHandler<OrderCommand>[] newEventHandlersArray(int size) {
    return new EventHandler[size];
}
 
Example #30
Source File: EventHandlerChain.java    From disruptor-spring-manager with MIT License 4 votes vote down vote up
private void printEventHandlers(StringJoiner str, EventHandler<T>[] eventHandlers) {
	for(int j=0;j<eventHandlers.length;j++){
		str.add(eventHandlers[j].getClass().getSimpleName());
	}
}