Disruptor是一个高性能的异步处理框架,或者可以认为是线程间通信的高效低延时的内存消息组件,它最大特点是高性能,其LMAX架构可以获得每秒6百万订单,用1微秒的延迟获得吞吐量为100K+。
关于Disruptor,可以参考http://ifeve.com/disruptor/。根据Disruptor优点,写了一个demo,仅供参考。下面直接贴代码。
package com.bosssoft.nontax.agency.invoice.servicecore; import reactor.jarjar.com.lmax.disruptor.*; import reactor.jarjar.com.lmax.disruptor.dsl.Disruptor; import reactor.jarjar.com.lmax.disruptor.dsl.EventHandlerGroup; import reactor.jarjar.com.lmax.disruptor.dsl.ProducerType; import java.util.Iterator; import java.util.Map; import java.util.concurrent.*; public class DisruptorTest { /** * 消息事件类 */ public static class MessageEvent{ /* * 消息ID */ private long id; /** * 原始消息 */ private String message; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } /** * 消息事件工厂类 */ public static class MessageEventFactory implements EventFactory<MessageEvent> { @Override public MessageEvent newInstance() { return new MessageEvent(); } } /** * 消息转换类,负责将消息转换为事件 */ public static class MessageEventTranslator implements EventTranslatorOneArg<MessageEvent,String> { @Override public void translateTo(MessageEvent messageEvent, long l, String s) { messageEvent.setMessage(s); messageEvent.setId(l); } } /** * 消息转换类,负责将消息转换为事件 */ public static class MessageEventTranslator2 implements EventTranslator<MessageEvent> { private String message; public MessageEventTranslator2(String message){ this.message = message; } @Override public void translateTo(MessageEvent messageEvent, long l) { messageEvent.setMessage(message); messageEvent.setId(l); } } /** * 消息事件处理类1,这里只打印消息 */ public static class MessageEventHandler implements EventHandler<MessageEvent> ,WorkHandler<MessageEvent>{ @Override public void onEvent(MessageEvent messageEvent, long l, boolean b) throws Exception { System.out.println("消息处理1:" + messageEvent.getMessage() + "的id为:" + messageEvent.getId()); } @Override public void onEvent(MessageEvent messageEvent) throws Exception { //System.out.println("消息处理1:" + messageEvent.getMessage() + "的id为:" + messageEvent.getId()); } } /** * 消息事件处理类2,这里只打印消息 */ public static class MessageEventHandler2 implements EventHandler<MessageEvent> ,WorkHandler<MessageEvent>{ @Override public void onEvent(MessageEvent messageEvent, long l, boolean b) throws Exception { System.out.println("消息处理2:" + messageEvent.getMessage() + "的id为:" + messageEvent.getId()); } @Override public void onEvent(MessageEvent messageEvent) throws Exception { //System.out.println("消息处理2:" + messageEvent.getMessage() + "的id为:" + messageEvent.getId()); } } /** * 消息事件处理类3,这里只打印消息 */ public static class MessageEventHandler3 implements EventHandler<MessageEvent> ,WorkHandler<MessageEvent>{ @Override public void onEvent(MessageEvent messageEvent, long l, boolean b) throws Exception { System.out.println("消息处理3:" + messageEvent.getMessage() + "的id为:" + messageEvent.getId()); } @Override public void onEvent(MessageEvent messageEvent) throws Exception { //System.out.println("消息处理3:" + messageEvent.getMessage() + "的id为:" + messageEvent.getId()); } } /** * 消息生产者类 */ public static class MessageEventProducer{ private RingBuffer<MessageEvent> ringBuffer; public MessageEventProducer(RingBuffer<MessageEvent> ringBuffer) { this.ringBuffer = ringBuffer; } /** * 将接收到的消息输出到ringBuffer * @param message */ public void onData(String message){ EventTranslatorOneArg<MessageEvent,String> translator = new MessageEventTranslator(); ringBuffer.publishEvent(translator,message); } } /** * 消息发布线程 */ public static class MessagePublisher implements Runnable { Disruptor<MessageEvent> disruptor; private CountDownLatch latch; private static int LOOP = 10000000;//模拟一千万次交易的发生 public MessagePublisher(CountDownLatch latch, Disruptor<MessageEvent> disruptor) { this.disruptor = disruptor; this.latch = latch; } @Override public void run() { for (int i = 0; i < LOOP; i++) { MessageEventTranslator2 messageEventTranslator = new MessageEventTranslator2("我是第"+i+"消息啊"); disruptor.publishEvent(messageEventTranslator); } latch.countDown(); } } public static void main(String[] args) throws InterruptedException { long beginTime = System.currentTimeMillis(); int bufferSize = 1024; ExecutorService executor=Executors.newFixedThreadPool(4); Disruptor<MessageEvent> disruptor = new Disruptor<MessageEvent>(new EventFactory<MessageEvent>() { @Override public MessageEvent newInstance() { return new MessageEvent(); } },bufferSize, executor, ProducerType.SINGLE, new BusySpinWaitStrategy()); disruptor.handleEventsWith(new MessageEventHandler(),new MessageEventHandler2()).then(new MessageEventHandler3()); disruptor.start(); CountDownLatch latch = new CountDownLatch(1); executor.submit(new MessagePublisher(latch, disruptor)); latch.await();//等待生产者完事. disruptor.shutdown(); executor.shutdown(); System.out.println("总耗时:"+(System.currentTimeMillis()-beginTime)); /* String message = "Hello Disruptor!"; String message2 = "Hello Disruptor2!"; int ringBufferSize = 1024;//必须是2的N次方 ExecutorService executor = Executors.newFixedThreadPool(3); Disruptor<MessageEvent> disruptor = new Disruptor<MessageEvent>(new MessageEventFactory(),ringBufferSize,executor,ProducerType.MULTI,new BlockingWaitStrategy()); diamond(disruptor); RingBuffer<MessageEvent> ringBuffer = disruptor.getRingBuffer(); ringBuffer.publishEvent(new MessageEventTranslator(),message); ringBuffer.publishEvent(new MessageEventTranslator(),message2); executor.shutdown();*/ /* String message = "Hello Disruptor!"; int ringBufferSize = 1024;//必须是2的N次方 ExecutorService executor = Executors.newFixedThreadPool(8); Disruptor<MessageEvent> disruptor = new Disruptor<MessageEvent>(new MessageEventFactory(),ringBufferSize,executor,ProducerType.MULTI,new BlockingWaitStrategy()); disruptor.handleEventsWith(new MessageEventHandler()); RingBuffer<MessageEvent> ringBuffer = disruptor.start(); MessageEventProducer producer = new MessageEventProducer(ringBuffer); producer.onData(message); executor.shutdown();*/ /*Map<String, String> map = System.getenv(); for(Iterator<String> itr = map.keySet().iterator(); itr.hasNext();){ String key = itr.next(); System.out.println(key + "=" + map.get(key)); }*/ } public static void parallel(Disruptor<MessageEvent> disruptor){ disruptor.handleEventsWith(new MessageEventHandler(),new MessageEventHandler2()); disruptor.start(); } public static void serial(Disruptor<MessageEvent> disruptor){ disruptor.handleEventsWith(new MessageEventHandler()).then(new MessageEventHandler2()); disruptor.start(); } public static void diamond(Disruptor<MessageEvent> disruptor){ disruptor.handleEventsWith(new MessageEventHandler(),new MessageEventHandler2()).then(new MessageEventHandler3()); disruptor.start(); } public static void serialWithPool(Disruptor<MessageEvent> disruptor){ disruptor.handleEventsWithWorkerPool(new MessageEventHandler(),new MessageEventHandler()).then(new MessageEventHandler2(),new MessageEventHandler2()); disruptor.start(); } }