本文整理汇总了Java中com.lmax.disruptor.EventHandler类的典型用法代码示例。如果您正苦于以下问题:Java EventHandler类的具体用法?Java EventHandler怎么用?Java EventHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
EventHandler类属于com.lmax.disruptor包,在下文中一共展示了EventHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: SharedMessageStore
import com.lmax.disruptor.EventHandler; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public SharedMessageStore(MessageDao messageDao, int bufferSize, int maxDbBatchSize) {
pendingMessages = new ConcurrentHashMap<>();
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("DisruptorMessageStoreThread-%d").build();
disruptor = new Disruptor<>(DbOperation.getFactory(),
bufferSize, namedThreadFactory, ProducerType.MULTI, new SleepingWaitStrategy());
disruptor.setDefaultExceptionHandler(new LogExceptionHandler());
disruptor.handleEventsWith(new DbEventMatcher(bufferSize))
.then(new DbWriter(messageDao, maxDbBatchSize))
.then((EventHandler<DbOperation>) (event, sequence, endOfBatch) -> event.clear());
disruptor.start();
this.messageDao = messageDao;
}
示例2: MultiBufferBatchEventProcessor
import com.lmax.disruptor.EventHandler; //导入依赖的package包/类
public MultiBufferBatchEventProcessor(
DataProvider<T>[] providers,
SequenceBarrier[] barriers,
EventHandler<T> handler)
{
if (providers.length != barriers.length)
{
throw new IllegalArgumentException();
}
this.providers = providers;
this.barriers = barriers;
this.handler = handler;
this.sequences = new Sequence[providers.length];
for (int i = 0; i < sequences.length; i++)
{
sequences[i] = new Sequence(-1);
}
}
示例3: shouldWaitOnAllProducersJoinedByAnd
import com.lmax.disruptor.EventHandler; //导入依赖的package包/类
@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);
}
示例4: shouldSupportCustomProcessorsAsDependencies
import com.lmax.disruptor.EventHandler; //导入依赖的package包/类
@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);
disruptor.after(processor).handleEventsWith(handlerWithBarrier);
ensureTwoEventsProcessedAccordingToDependencies(countDownLatch, delayedEventHandler);
}
示例5: shouldSupportHandlersAsDependenciesToCustomProcessors
import com.lmax.disruptor.EventHandler; //导入依赖的package包/类
@Test
public void shouldSupportHandlersAsDependenciesToCustomProcessors()
throws Exception
{
final DelayedEventHandler delayedEventHandler = createDelayedEventHandler();
disruptor.handleEventsWith(delayedEventHandler);
RingBuffer<TestEvent> ringBuffer = disruptor.getRingBuffer();
CountDownLatch countDownLatch = new CountDownLatch(2);
EventHandler<TestEvent> handlerWithBarrier = new EventHandlerStub<TestEvent>(countDownLatch);
final SequenceBarrier sequenceBarrier = disruptor.after(delayedEventHandler).asSequenceBarrier();
final BatchEventProcessor<TestEvent> processor =
new BatchEventProcessor<TestEvent>(ringBuffer, sequenceBarrier, handlerWithBarrier);
disruptor.handleEventsWith(processor);
ensureTwoEventsProcessedAccordingToDependencies(countDownLatch, delayedEventHandler);
}
示例6: shouldSupportCustomProcessorsAndHandlersAsDependencies
import com.lmax.disruptor.EventHandler; //导入依赖的package包/类
@Test
public void shouldSupportCustomProcessorsAndHandlersAsDependencies() throws Exception
{
final DelayedEventHandler delayedEventHandler1 = createDelayedEventHandler();
final DelayedEventHandler delayedEventHandler2 = createDelayedEventHandler();
disruptor.handleEventsWith(delayedEventHandler1);
RingBuffer<TestEvent> ringBuffer = disruptor.getRingBuffer();
CountDownLatch countDownLatch = new CountDownLatch(2);
EventHandler<TestEvent> handlerWithBarrier = new EventHandlerStub<TestEvent>(countDownLatch);
final SequenceBarrier sequenceBarrier = disruptor.after(delayedEventHandler1).asSequenceBarrier();
final BatchEventProcessor<TestEvent> processor =
new BatchEventProcessor<TestEvent>(ringBuffer, sequenceBarrier, delayedEventHandler2);
disruptor.after(delayedEventHandler1).and(processor).handleEventsWith(handlerWithBarrier);
ensureTwoEventsProcessedAccordingToDependencies(countDownLatch, delayedEventHandler1, delayedEventHandler2);
}
示例7: shouldMakeEntriesAvailableToFirstCustomProcessorsImmediately
import com.lmax.disruptor.EventHandler; //导入依赖的package包/类
@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);
}
示例8: shouldHonourDependenciesForCustomProcessors
import com.lmax.disruptor.EventHandler; //导入依赖的package包/类
@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);
}
示例9: Source
import com.lmax.disruptor.EventHandler; //导入依赖的package包/类
/**
* 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();
}
示例10: testLaterStartConsumer
import com.lmax.disruptor.EventHandler; //导入依赖的package包/类
@Test
public void testLaterStartConsumer() throws InterruptedException {
System.out.println("!!!!!!!!!!!!!!!Begin testLaterStartConsumer!!!!!!!!!!");
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);
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 testLaterStartConsumer!!!!!!!!!!");
}
示例11: testSingleProducer
import com.lmax.disruptor.EventHandler; //导入依赖的package包/类
@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!!!!!!!!!!!!!!");
}
示例12: test_publish_single_eventprocessor_topology
import com.lmax.disruptor.EventHandler; //导入依赖的package包/类
/**
* 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";
}
});
}
示例13: test_publish_simple_eventprocessor_topology
import com.lmax.disruptor.EventHandler; //导入依赖的package包/类
/**
* 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";
}
});
}
示例14: test_publish_diamond_eventprocessor_topology
import com.lmax.disruptor.EventHandler; //导入依赖的package包/类
/**
* 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";
}
});
}
示例15: test_publish_complicated_diamond_eventprocessor_topology
import com.lmax.disruptor.EventHandler; //导入依赖的package包/类
/**
* 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";
}
});
}