依赖 : disruptor-2.10.4.jar
package com.lmax.disruptor;
/**
* User: caiyuan
* Date: 13-2-18 16:56
*/
public final class DispatchEventHandler<T> implements EventHandler<T>, LifecycleAware {
private final EventHandler<T>[] eventHandlers;
private final int size;
private int index = -1;
public DispatchEventHandler(final EventHandler<T>... eventHandlers) {
this.eventHandlers = eventHandlers;
this.size = eventHandlers.length;
}
@Override
public void onEvent(final T event, final long sequence, final boolean endOfBatch) throws Exception {
if (index == size || index == -1) index = 0;
EventHandler<T> eventHandler = eventHandlers[index];
eventHandler.onEvent(event, sequence, endOfBatch);
index = index + 1;
}
@Override
public void onStart() {
for (final EventHandler<T> eventHandler : eventHandlers) {
if (eventHandler instanceof LifecycleAware) {
((LifecycleAware) eventHandler).onStart();
}
}
}
@Override
public void onShutdown() {
for (final EventHandler<T> eventHandler : eventHandlers) {
if (eventHandler instanceof LifecycleAware) {
((LifecycleAware) eventHandler).onShutdown();
}
}
}
}
package com.lmax.disruptor;
/**
* POJO
*/
public class SimpleEvent {
private String value;
public String getValue() {
return value;
}
public void setValue(final String value) {
this.value = value;
}
}
package com.lmax.disruptor;
/**
* 事件生产者
*/
public class SimpleEventFactory {
public final static EventFactory<SimpleEvent> EVENT_FACTORY = new EventFactory<SimpleEvent>() {
public SimpleEvent newInstance() {
return new SimpleEvent();
}
};
}
package com.lmax.disruptor;
/**
* 事件处理器
*/
public class SimpleEventHandler implements EventHandler<SimpleEvent> {
private RingBuffer<SimpleEvent> ringBuffer;
public SimpleEventHandler(RingBuffer<SimpleEvent> ringBuffer) {
this.ringBuffer = ringBuffer;
}
@Override
public void onEvent(SimpleEvent event, long sequence, boolean endOfBatch) throws Exception {
if (ringBuffer != null) {
String value = event.getValue();
long index = ringBuffer.next();
SimpleEvent entity = ringBuffer.get(index);
entity.setValue(value + "B");
ringBuffer.publish(index);
} else {
System.out.println(sequence +"\t" +event.getValue());
}
}
}
package com.lmax.disruptor;
/**
* 装载事件,事件处理器 和 RingBuffer
*/
public class SimpleTest {
public static void main(String[] args) {
final RingBuffer<SimpleEvent> rb1 = new RingBuffer<SimpleEvent>(
SimpleEventFactory.EVENT_FACTORY,
new MultiThreadedClaimStrategy(2048),
new YieldingWaitStrategy());
final RingBuffer<SimpleEvent> rb2 = new RingBuffer<SimpleEvent>(
SimpleEventFactory.EVENT_FACTORY,
new MultiThreadedClaimStrategy(2048),
new YieldingWaitStrategy());
EventHandler seh1 = new SimpleEventHandler(null);
EventHandler seh2 = new SimpleEventHandler(null);
EventHandler seh3 = new SimpleEventHandler(rb2);
EventHandler seh4 = new SimpleEventHandler(null);
EventHandler seh5 = new SimpleEventHandler(null);
final EventHandler<SimpleEvent> eh1 = new DispatchEventHandler<SimpleEvent>(seh1, seh2);
final EventHandler<SimpleEvent> eh2 = new AggregateEventHandler<SimpleEvent>(eh1, seh3);
final EventHandler<SimpleEvent> eh3 = new AggregateEventHandler<SimpleEvent>(seh4, seh5);
final EventProcessor ep1 = new BatchEventProcessor<SimpleEvent>(
rb1,
rb1.newBarrier(),
eh2);
final EventProcessor ep2 = new BatchEventProcessor<SimpleEvent>(
rb2,
rb2.newBarrier(),
eh3);
rb1.setGatingSequences(ep1.getSequence());
rb2.setGatingSequences(ep2.getSequence());
final long start = System.currentTimeMillis();
new Thread(ep1, "EP1").start();
new Thread(ep2, "EP2").start();
new Thread(new Runnable() {
@Override
public void run() {
int count = 0;
while (count < 9000000) {
long sequence = rb1.next();
SimpleEvent event = rb1.get(sequence);
event.setValue("A");
rb1.publish(sequence);
count = count + 1;
}
System.out.println("T" + (System.currentTimeMillis() - start));
}
}, "P1").start();
}
}
输出:
0 A
1 A
2 A
0 AB
3 A
0 AB
4 A
5 A
6 A
7 A
8 A
9 A
10 A
1 AB
11 A
1 AB
12 A
13 A
....