package com.zero.disruptor; import com.lmax.disruptor.EventFactory; public class SimpleEvent { private String value; public void setValue(String value) { this.value = value; } public String getValue() { return value; } public static final EventFactory<SimpleEvent> FACTORY = new EventFactory<SimpleEvent>() { @Override public SimpleEvent newInstance() { return new SimpleEvent(); } }; }2. 定义事件处理的具体实现
package com.zero.disruptor; import java.util.concurrent.TimeUnit; import com.lmax.disruptor.EventHandler; import com.lmax.disruptor.WorkHandler; /** * EventHandlers是BatchEventProcessor要用到的, WorkHandler是WorkerPool要用到的。为简便放在了一起 */ public class SimpleHandler implements EventHandler<SimpleEvent>, WorkHandler<SimpleEvent> { private String handlerName; public SimpleHandler(String handlerName) { this.handlerName = handlerName; } @Override // Override---EventHandler public void onEvent(SimpleEvent event, long sequence, boolean endOfBatch) throws Exception { TimeUnit.MILLISECONDS.sleep(100); System.out.println(handlerName + " before deal : sequence = " + sequence + ", Event: " + event.getValue()); event.setValue(event.getValue() + "--" + handlerName); System.out.println(handlerName + " after deal : " + "Event: " + event.getValue()); } @Override // Override---WorkHandler public void onEvent(SimpleEvent event) throws Exception { // TODO Auto-generated method stub TimeUnit.MILLISECONDS.sleep(100); System.out.println(handlerName + " before deal : Event: " + event.getValue()); event.setValue(event.getValue() + "--" + handlerName); System.out.println(handlerName + " after deal : " + " Event: " + event.getValue()); } }3. 等待策略
long seq = 0; for (int i = 0; i < 20; i++) { try { seq = ringBuffer.next();// 占个坑 ,ringBuffer一个可用区块 ringBuffer.get(seq).setValue((long) (Math.random() * 9999));// 给这个区块放入数据 } finally { ringBuffer.publish(seq);// 发布这个区块的数据使handler(consumer)可见 System.out.println("Producer " + seq); } }Disruptor 还提供另外一种形式的调用来简化以上操作,并确保 publish 总是得到调用。
package com.zero.disruptor; import java.nio.ByteBuffer; import com.lmax.disruptor.EventTranslatorOneArg; import com.lmax.disruptor.RingBuffer; public class SimpleEventProducer { private final RingBuffer<SimpleEvent> ringBuffer; public SimpleEventProducer(RingBuffer<SimpleEvent> ringBuffer) { this.ringBuffer = ringBuffer; } private static final EventTranslatorOneArg TRANSLATOR = new EventTranslatorOneArg<SimpleEvent, String>() { @Override public void translateTo(SimpleEvent event, long sequence, String value) { System.out.println("Producer --- " + sequence + ", event = " + value); event.setValue(value); } }; /** * onData用来发布事件 * * @param value */ public void onData(final String value) { ringBuffer.publishEvent(TRANSLATOR, value); } }5. 关闭 Disruptor
disruptor.shutdown();//关闭 disruptor,方法会堵塞,直至所有的事件都得到处理; executor.shutdown();//关闭 disruptor 使用的线程池;如果需要的话,必须手动关闭, disruptor 在 shutdown 时不会自动关闭;6.示例1:
public static void demo0() throws Exception { /* * createSingleProducer创建一个单生产者的RingBuffer, * 第一个参数叫EventFactory,从名字上理解就是“事件工厂”,其实它的职责就是产生数据填充RingBuffer的区块。 * 第二个参数是RingBuffer的大小,它必须是2的指数倍 目的是为了将求模运算转为&运算提高效率 * 第三个参数是RingBuffer的生产都在没有可用区块的时候(可能是消费者(或者说是事件处理器) 太慢了)的等待策略 */ final RingBuffer<SimpleEvent> ringBuffer = RingBuffer .createSingleProducer(SimpleEvent.FACTORY, 2, new SleepingWaitStrategy()); // 创建线程池 ExecutorService executors = Executors.newCachedThreadPool(); // 创建SequenceBarrier SequenceBarrier sequenceBarrier = ringBuffer.newBarrier(); // 创建消息处理器 ,消费者 BatchEventProcessor<SimpleEvent> processor1 = new BatchEventProcessor<SimpleEvent>( ringBuffer, sequenceBarrier, new SimpleHandler( "EventHandler_01")); // 传入所有消费者线程的序号,如果只有一个消费者的情况可以省略 ringBuffer.addGatingSequences(processor1.getSequence()); // 把消息处理器提交到线程池 executors.submit(processor1); // 如果存在多个消费者 那重复执行上面3行代码 把SimpleHandler换成其它消费者类 BatchEventProcessor<SimpleEvent> processor2 = new BatchEventProcessor<SimpleEvent>( ringBuffer, sequenceBarrier, new SimpleHandler( "EventHandler_02")); ringBuffer.addGatingSequences(processor2.getSequence()); executors.submit(processor2); // 两个消费者会消费一样的事件 // 生产者 SimpleEventProducer producer = new SimpleEventProducer(ringBuffer); for (int i = 0; i < 4; i++) { producer.onData(String.valueOf(i)); } processor1.halt();// 通知事件(或者说消息)处理器可以结束了(并不是马上结束!!!) processor2.halt(); executors.shutdown();// 终止线程 }运行结果:
Producer --- 0, event = 0 Producer --- 1, event = 1 EventHandler_02 before deal : sequence = 0, Event: 0 EventHandler_02 after deal : Event: 0--EventHandler_02 EventHandler_01 before deal : sequence = 0, Event: 0--EventHandler_02 EventHandler_01 after deal : Event: 0--EventHandler_02--EventHandler_01 EventHandler_02 before deal : sequence = 1, Event: 1 EventHandler_02 after deal : Event: 1--EventHandler_02 EventHandler_01 before deal : sequence = 1, Event: 1--EventHandler_02 EventHandler_01 after deal : Event: 1--EventHandler_02--EventHandler_01 Producer --- 2, event = 2 Producer --- 3, event = 3 EventHandler_01 before deal : sequence = 2, Event: 2 EventHandler_01 after deal : Event: 2--EventHandler_01 EventHandler_02 before deal : sequence = 2, Event: 2--EventHandler_01 EventHandler_02 after deal : Event: 2--EventHandler_01--EventHandler_02 EventHandler_01 before deal : sequence = 3, Event: 3 EventHandler_01 after deal : Event: 3--EventHandler_01 EventHandler_02 before deal : sequence = 3, Event: 3--EventHandler_01 EventHandler_02 after deal : Event: 3--EventHandler_01--EventHandler_02
可以看出,每个event都被处理了,但是先后顺序不同,如果需要有先后顺序,可以采用一下方式:
public static void demo1() throws Exception { final RingBuffer<SimpleEvent> ringBuffer = RingBuffer .createSingleProducer(SimpleEvent.FACTORY, 2, new SleepingWaitStrategy()); ExecutorService executors = Executors.newCachedThreadPool(); SequenceBarrier stepOneSequenceBarrier = ringBuffer.newBarrier(); BatchEventProcessor<SimpleEvent> processor1 = new BatchEventProcessor<SimpleEvent>( ringBuffer, stepOneSequenceBarrier, new SimpleHandler( "EventHandler_01")); executors.submit(processor1); SequenceBarrier stepTwoSequenceBarrier = ringBuffer .newBarrier(processor1.getSequence()); BatchEventProcessor<SimpleEvent> processor2 = new BatchEventProcessor<SimpleEvent>( ringBuffer, stepTwoSequenceBarrier, new SimpleHandler( "EventHandler_02")); //processor2消费最慢,所以以processor2的Sequence为阈值 ringBuffer.addGatingSequences(processor2.getSequence()); executors.submit(processor2); // 生产者 SimpleEventProducer producer = new SimpleEventProducer(ringBuffer); for (int i = 0; i < 4; i++) { producer.onData(String.valueOf(i)); } executors.shutdown();// 终止线程 }运行结果:
Producer --- 0, event = 0 Producer --- 1, event = 1 EventHandler_01 before deal : sequence = 0, Event: 0 EventHandler_01 after deal : Event: 0--EventHandler_01 EventHandler_01 before deal : sequence = 1, Event: 1 EventHandler_01 after deal : Event: 1--EventHandler_01 EventHandler_02 before deal : sequence = 0, Event: 0--EventHandler_01 EventHandler_02 after deal : Event: 0--EventHandler_01--EventHandler_02 EventHandler_02 before deal : sequence = 1, Event: 1--EventHandler_01 EventHandler_02 after deal : Event: 1--EventHandler_01--EventHandler_02 Producer --- 2, event = 2 Producer --- 3, event = 3 EventHandler_01 before deal : sequence = 2, Event: 2 EventHandler_01 after deal : Event: 2--EventHandler_01 EventHandler_01 before deal : sequence = 3, Event: 3 EventHandler_01 after deal : Event: 3--EventHandler_01 EventHandler_02 before deal : sequence = 2, Event: 2--EventHandler_01 EventHandler_02 after deal : Event: 2--EventHandler_01--EventHandler_02 EventHandler_02 before deal : sequence = 3, Event: 3--EventHandler_01 EventHandler_02 after deal : Event: 3--EventHandler_01--EventHandler_02如果想一个消息只被一个消费者消费呢?
public static void demo2() throws Exception { final RingBuffer<SimpleEvent> ringBuffer = RingBuffer .createSingleProducer(SimpleEvent.FACTORY, 4, new SleepingWaitStrategy()); ExecutorService executors = Executors.newCachedThreadPool(); SequenceBarrier sequenceBarrier = ringBuffer.newBarrier(); // 创建消息处理器 ,消费者 WorkHandler<SimpleEvent> processor1 = new SimpleHandler("WorkHandler_1"); WorkHandler<SimpleEvent> processor2 = new SimpleHandler("WorkHandler_2"); WorkerPool<SimpleEvent> workerPool = new WorkerPool<SimpleEvent>( ringBuffer, sequenceBarrier, new IgnoreExceptionHandler(), processor1, processor2); ringBuffer.addGatingSequences(workerPool.getWorkerSequences());// 要在workerPool.start(executors);之前 workerPool.start(executors); // 生产者 SimpleEventProducer producer = new SimpleEventProducer(ringBuffer); for (int i = 0; i < 4; i++) { producer.onData(String.valueOf(i)); } workerPool.halt(); executors.shutdown();// 终止线程 }运行结果:
Producer --- 0, event = 0 Producer --- 1, event = 1 Producer --- 2, event = 2 Producer --- 3, event = 3 WorkHandler_2 before deal : Event: 1 WorkHandler_1 before deal : Event: 0 WorkHandler_1 after deal : Event: 0--WorkHandler_1 WorkHandler_2 after deal : Event: 1--WorkHandler_2 WorkHandler_1 before deal : Event: 2 WorkHandler_2 before deal : Event: 3 WorkHandler_2 after deal : Event: 3--WorkHandler_2 WorkHandler_1 after deal : Event: 2--WorkHandler_1如果想一个消息被两组消费者消费,但是每组只能有一个消费?
public static void demo3() throws Exception { final RingBuffer<SimpleEvent> ringBuffer = RingBuffer .createSingleProducer(SimpleEvent.FACTORY, 4, new SleepingWaitStrategy()); ExecutorService executors = Executors.newCachedThreadPool(); SequenceBarrier sequenceBarrier = ringBuffer.newBarrier(); // 创建消息处理器 ,消费者 WorkHandler<SimpleEvent> processor1 = new SimpleHandler("WorkHandler_1"); WorkHandler<SimpleEvent> processor2 = new SimpleHandler("WorkHandler_2"); WorkerPool<SimpleEvent> workerPool = new WorkerPool<SimpleEvent>( ringBuffer, sequenceBarrier, new IgnoreExceptionHandler(), processor1, processor2); ringBuffer.addGatingSequences(workerPool.getWorkerSequences());// 要在workerPool.start(executors);之前 workerPool.start(executors); // 创建消息处理器 ,消费者 WorkerPool<SimpleEvent> workerPool2 = new WorkerPool<SimpleEvent>( ringBuffer, sequenceBarrier, new IgnoreExceptionHandler(), new SimpleHandler("WorkHandler_3")); ringBuffer.addGatingSequences(workerPool2.getWorkerSequences());// 要在workerPool.start(executors);之前 workerPool2.start(executors); // 生产者 SimpleEventProducer producer = new SimpleEventProducer(ringBuffer); for (int i = 0; i < 4; i++) { producer.onData(String.valueOf(i)); } workerPool.halt(); executors.shutdown();// 终止线程 }运行结果:
Producer --- 0, event = 0 Producer --- 1, event = 1 Producer --- 2, event = 2 Producer --- 3, event = 3 WorkHandler_2 before deal : Event: 1 WorkHandler_3 before deal : Event: 0 WorkHandler_3 after deal : Event: 0--WorkHandler_3 WorkHandler_1 before deal : Event: 0 WorkHandler_1 after deal : Event: 0--WorkHandler_3--WorkHandler_1 WorkHandler_2 after deal : Event: 1--WorkHandler_2 WorkHandler_2 before deal : Event: 3 WorkHandler_1 before deal : Event: 2 WorkHandler_1 after deal : Event: 2--WorkHandler_1 WorkHandler_2 after deal : Event: 3--WorkHandler_2 WorkHandler_3 before deal : Event: 1--WorkHandler_2 WorkHandler_3 after deal : Event: 1--WorkHandler_2--WorkHandler_3 WorkHandler_3 before deal : Event: 2--WorkHandler_1 WorkHandler_3 after deal : Event: 2--WorkHandler_1--WorkHandler_3 WorkHandler_3 before deal : Event: 3--WorkHandler_2 WorkHandler_3 after deal : Event: 3--WorkHandler_2--WorkHandler_3
public static void work1() { ExecutorService executor = Executors.newFixedThreadPool(2); int ringBufferSize = 2; // RingBuffer 大小,必须是 2 的 N 次方; // 构造Disruptor Disruptor<SimpleEvent> disruptor = new Disruptor<SimpleEvent>( SimpleEvent.FACTORY, ringBufferSize, executor, ProducerType.SINGLE, new BlockingWaitStrategy()); disruptor.handleEventsWith(new SimpleHandler("BatchEventHandler_01"), new SimpleHandler("BatchEventHandler_02")); // 每个EventHandler占用一个线程,如果Executors.newFixedThreadPool(1);会导致阻塞 RingBuffer<SimpleEvent> ringBuffer = disruptor.start(); // 生产者 SimpleEventProducer producer = new SimpleEventProducer(ringBuffer); for (long i = 10; i < 15; i++) { producer.onData(String.valueOf(i)); } // 关闭 Disruptor disruptor.shutdown();// 关闭 disruptor,方法会堵塞,直至所有的事件都得到处理; executor.shutdown();// 关闭 disruptor 使用的线程池;如果需要的话,必须手动关闭, // disruptor在shutdown 时不会自动关闭; }以上示例,效果等同于demo0。运行结果;
Producer --- 0, event = 10 Producer --- 1, event = 11 BatchEventHandler_02 before deal : sequence = 0, Event: 10 BatchEventHandler_02 after deal : Event: 10--BatchEventHandler_02 BatchEventHandler_01 before deal : sequence = 0, Event: 10 BatchEventHandler_01 after deal : Event: 10--BatchEventHandler_02--BatchEventHandler_01 BatchEventHandler_01 before deal : sequence = 1, Event: 11 BatchEventHandler_01 after deal : Event: 11--BatchEventHandler_01 BatchEventHandler_02 before deal : sequence = 1, Event: 11--BatchEventHandler_01 BatchEventHandler_02 after deal : Event: 11--BatchEventHandler_01--BatchEventHandler_02 Producer --- 2, event = 12 Producer --- 3, event = 13 BatchEventHandler_01 before deal : sequence = 2, Event: 12 BatchEventHandler_02 before deal : sequence = 2, Event: 12 BatchEventHandler_02 after deal : Event: 12--BatchEventHandler_02 BatchEventHandler_01 after deal : Event: 12--BatchEventHandler_02--BatchEventHandler_01 BatchEventHandler_02 before deal : sequence = 3, Event: 13 BatchEventHandler_02 after deal : Event: 13--BatchEventHandler_02 BatchEventHandler_01 before deal : sequence = 3, Event: 13--BatchEventHandler_02 BatchEventHandler_01 after deal : Event: 13--BatchEventHandler_02--BatchEventHandler_01 Producer --- 4, event = 14 BatchEventHandler_02 before deal : sequence = 4, Event: 14 BatchEventHandler_02 after deal : Event: 14--BatchEventHandler_02 BatchEventHandler_01 before deal : sequence = 4, Event: 14--BatchEventHandler_02 BatchEventHandler_01 after deal : Event: 14--BatchEventHandler_02--BatchEventHandler_01可以看出每个SimpleHandler (implements EventHandler)都会处理同一条消息。另外还可以使用类似:
disruptor.handleEventsWith(new SimpleHandler("BatchEventHandler_01")) .then(new SimpleHandler("BatchEventHandler_02"));这样的方法来定义依赖关系,比如先执行哪个handler再执行哪个handler。
Producer --- 0, event = 10 Producer --- 1, event = 11 BatchEventHandler_01 before deal : sequence = 0, Event: 10 BatchEventHandler_01 after deal : Event: 10--BatchEventHandler_01 BatchEventHandler_01 before deal : sequence = 1, Event: 11 BatchEventHandler_01 after deal : Event: 11--BatchEventHandler_01 BatchEventHandler_02 before deal : sequence = 0, Event: 10--BatchEventHandler_01 BatchEventHandler_02 after deal : Event: 10--BatchEventHandler_01--BatchEventHandler_02 BatchEventHandler_02 before deal : sequence = 1, Event: 11--BatchEventHandler_01 BatchEventHandler_02 after deal : Event: 11--BatchEventHandler_01--BatchEventHandler_02 Producer --- 2, event = 12 Producer --- 3, event = 13 BatchEventHandler_01 before deal : sequence = 2, Event: 12 BatchEventHandler_01 after deal : Event: 12--BatchEventHandler_01 BatchEventHandler_01 before deal : sequence = 3, Event: 13 BatchEventHandler_01 after deal : Event: 13--BatchEventHandler_01 BatchEventHandler_02 before deal : sequence = 2, Event: 12--BatchEventHandler_01 BatchEventHandler_02 after deal : Event: 12--BatchEventHandler_01--BatchEventHandler_02 BatchEventHandler_02 before deal : sequence = 3, Event: 13--BatchEventHandler_01 BatchEventHandler_02 after deal : Event: 13--BatchEventHandler_01--BatchEventHandler_02 Producer --- 4, event = 14 BatchEventHandler_01 before deal : sequence = 4, Event: 14 BatchEventHandler_01 after deal : Event: 14--BatchEventHandler_01 BatchEventHandler_02 before deal : sequence = 4, Event: 14--BatchEventHandler_01 BatchEventHandler_02 after deal : Event: 14--BatchEventHandler_01--BatchEventHandler_02同样,还可以使用WorkerPool:
public static void work2() { ExecutorService executor = Executors.newFixedThreadPool(2); int ringBufferSize = 4; // RingBuffer 大小,必须是 2 的 N 次方; // 构造Disruptor Disruptor<SimpleEvent> disruptor = new Disruptor<SimpleEvent>( SimpleEvent.FACTORY, ringBufferSize, executor, ProducerType.SINGLE, new BlockingWaitStrategy()); disruptor.handleExceptionsWith(new IgnoreExceptionHandler()); disruptor.handleEventsWithWorkerPool(new SimpleHandler( "WorkEventHandler_01"), new SimpleHandler("WorkEventHandler_02")); RingBuffer<SimpleEvent> ringBuffer = disruptor.start(); // // 获取生产者的位置信息 SequenceBarrier sequenceBarrier = ringBuffer.newBarrier(); // 生产者 SimpleEventProducer producer = new SimpleEventProducer(ringBuffer); for (long i = 10; i < 15; i++) { producer.onData(String.valueOf(i)); } disruptor.shutdown(); executor.shutdown(); }运行结果:
Producer --- 0, event = 10 Producer --- 1, event = 11 Producer --- 2, event = 12 Producer --- 3, event = 13 WorkEventHandler_02 before deal : Event: 10 WorkEventHandler_01 before deal : Event: 11 WorkEventHandler_02 after deal : Event: 10--WorkEventHandler_02 WorkEventHandler_01 after deal : Event: 11--WorkEventHandler_01 Producer --- 4, event = 14 WorkEventHandler_02 before deal : Event: 12 WorkEventHandler_02 after deal : Event: 12--WorkEventHandler_02 WorkEventHandler_01 before deal : Event: 13 WorkEventHandler_01 after deal : Event: 13--WorkEventHandler_01 WorkEventHandler_02 before deal : Event: 14 WorkEventHandler_02 after deal : Event: 14--WorkEventHandler_02