1、概述java
disruptor对于处理并发任务很擅长,曾有人测过,一个线程里1s内能够处理六百万个订单,性能至关感人。缓存
这个框架的结构大概是:数据生产端 --> 缓存 --> 消费端并发
缓存中的数据是主动发给消费端的,而不是像通常的生产者消费者模式那样,消费端去缓存中取数据。框架
能够将disruptor理解为,基于事件驱动的高效队列、轻量级的JMS异步
disruptor学习网站:http://ifeve.com/disruptor-getting-startedide
2、开发流程性能
1.建Event类(数据对象)学习
2.创建一个生产数据的工厂类,EventFactory,用于生产数据;网站
3.监听事件类(处理Event数据)this
4.实例化Disruptor,配置参数,绑定事件;
5.建存放数据的核心 RingBuffer,生产的数据放入 RungBuffer。
3、HelloWord
1.入口
import java.nio.ByteBuffer; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import com.lmax.disruptor.RingBuffer; import com.lmax.disruptor.YieldingWaitStrategy; import com.lmax.disruptor.dsl.Disruptor; import com.lmax.disruptor.dsl.ProducerType; public class LongEventMain { public static void main(String[] args) throws Exception { //建立缓冲池 ExecutorService executor = Executors.newCachedThreadPool(); //建立工厂 LongEventFactory factory = new LongEventFactory(); //建立bufferSize ,也就是RingBuffer大小,必须是2的N次方 int ringBufferSize = 1024 * 1024; // /** //BlockingWaitStrategy 是最低效的策略,但其对CPU的消耗最小而且在各类不一样部署环境中能提供更加一致的性能表现 WaitStrategy BLOCKING_WAIT = new BlockingWaitStrategy(); //SleepingWaitStrategy 的性能表现跟BlockingWaitStrategy差很少,对CPU的消耗也相似,但其对生产者线程的影响最小,适合用于异步日志相似的场景 WaitStrategy SLEEPING_WAIT = new SleepingWaitStrategy(); //YieldingWaitStrategy 的性能是最好的,适合用于低延迟的系统。在要求极高性能且事件处理线数小于CPU逻辑核心数的场景中,推荐使用此策略;例如,CPU开启超线程的特性 WaitStrategy YIELDING_WAIT = new YieldingWaitStrategy(); */ //建立disruptor Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(factory, ringBufferSize, executor, ProducerType.SINGLE, new YieldingWaitStrategy()); // 链接消费事件方法 disruptor.handleEventsWith(new LongEventHandler()); // 启动 disruptor.start(); //Disruptor 的事件发布过程是一个两阶段提交的过程: //发布事件 RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer(); LongEventProducer producer = new LongEventProducer(ringBuffer); //LongEventProducerWithTranslator producer = new LongEventProducerWithTranslator(ringBuffer); ByteBuffer byteBuffer = ByteBuffer.allocate(8); for(long l = 0; l<100; l++){ byteBuffer.putLong(0, l); producer.onData(byteBuffer); //Thread.sleep(1000); } disruptor.shutdown();//关闭 disruptor,方法会堵塞,直至全部的事件都获得处理; executor.shutdown();//关闭 disruptor 使用的线程池;若是须要的话,必须手动关闭, disruptor 在 shutdown 时不会自动关闭; } }
2.数据对象:
public class LongEvent { private long value; public long getValue() { return value; } public void setValue(long value) { this.value = value; } }
3.Event工厂
import com.lmax.disruptor.EventFactory; // 须要让disruptor为咱们建立事件,咱们同时还声明了一个EventFactory来实例化Event对象。 public class LongEventFactory implements EventFactory { @Override public Object newInstance() { return new LongEvent(); } }
4.生产者
import java.nio.ByteBuffer; import com.lmax.disruptor.RingBuffer; /** * 很明显的是:当用一个简单队列来发布事件的时候会牵涉更多的细节,这是由于事件对象还须要预先建立。 * 发布事件最少须要两步:获取下一个事件槽并发布事件(发布事件的时候要使用try/finnally保证事件必定会被发布)。 * 若是咱们使用RingBuffer.next()获取一个事件槽,那么必定要发布对应的事件。 * 若是不能发布事件,那么就会引发Disruptor状态的混乱。 * 尤为是在多个事件生产者的状况下会致使事件消费者失速,从而不得不重启应用才能会恢复。 */ public class LongEventProducer { private final RingBuffer<LongEvent> ringBuffer; public LongEventProducer(RingBuffer<LongEvent> ringBuffer){ this.ringBuffer = ringBuffer; } /** * onData用来发布事件,每调用一次就发布一次事件 * 它的参数会用过事件传递给消费者 */ public void onData(ByteBuffer bb){ //1.能够把ringBuffer看作一个事件队列,那么next就是获得下面一个事件槽 long sequence = ringBuffer.next(); try { //2.用上面的索引取出一个空的事件用于填充(获取该序号对应的事件对象) LongEvent event = ringBuffer.get(sequence); //3.获取要经过事件传递的业务数据 event.setValue(bb.getLong(0)); } finally { //4.发布事件 //注意,最后的 ringBuffer.publish 方法必须包含在 finally 中以确保必须获得调用;若是某个请求的 sequence 未被提交,将会堵塞后续的发布操做或者其它的 producer。 ringBuffer.publish(sequence); } } }
5.消费者
import com.lmax.disruptor.EventHandler; //咱们还须要一个事件消费者,也就是一个事件处理器。这个事件处理器简单地把事件中存储的数据打印到终端: public class LongEventHandler implements EventHandler<LongEvent> { @Override public void onEvent(LongEvent longEvent, long l, boolean b) throws Exception { System.out.println(longEvent.getValue()); } }