Disruptor Quick Start

試著忘記壹切 提交于 2019-11-30 16:19:30

创建步骤:

  1. 简历一个工厂Event类,用于创建Event类实例对象
  2. 需要有一个监听事件类,用于处理数据(Event类)
  3. 实例化Disruptor实例,配置一系列参数,编写Disruptor核心组件
  4. 编写生产者组件,向Disruptor容器中去投递数据

简历一个工厂Event类,OrderEvent.java

package com.bfxy.disruptor.quickstart;

public class OrderEvent {
   //订单的价格 
   private long value; 

   public long getValue() {
      return value;
   }

   public void setValue(long value) {
      this.value = value;
   }
   
}

OrderEventFactory.java

package com.bfxy.disruptor.quickstart;

import com.lmax.disruptor.EventFactory;

public class OrderEventFactory implements EventFactory<OrderEvent>{

   public OrderEvent newInstance() {
      return new OrderEvent();      //这个方法就是为了返回空的数据对象(Event)
   }

}

2、需要有一个监听事件类:OrderEventHandler.java

package com.bfxy.disruptor.quickstart;

import com.lmax.disruptor.EventHandler;

public class OrderEventHandler implements EventHandler<OrderEvent>{

   public void onEvent(OrderEvent event, long sequence, boolean endOfBatch) throws Exception {
      Thread.sleep(Integer.MAX_VALUE);
      System.err.println("消费者: " + event.getValue());
   }

}

3、实例化Disruptor实例:Main.java

package com.bfxy.disruptor.quickstart;

import java.nio.ByteBuffer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

public class Main {

   public static void main(String[] args) {
      // 参数准备工作
      OrderEventFactory orderEventFactory = new OrderEventFactory();
      int ringBufferSize = 4;
      ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
      
      /**
       * 1 eventFactory: 消息(event)工厂对象
       * 2 ringBufferSize: 容器的长度
       * 3 executor: 线程池(建议使用自定义线程池) RejectedExecutionHandler
       * 4 ProducerType: 单生产者 还是 多生产者
       * 5 waitStrategy: 等待策略
       */
      //1. 实例化disruptor对象
      Disruptor<OrderEvent> disruptor = new Disruptor<OrderEvent>(orderEventFactory,
            ringBufferSize,
            executor,
            ProducerType.SINGLE,
            new BlockingWaitStrategy());
      
      //2. 添加消费者的监听 (构建disruptor 与 消费者的一个关联关系)
      disruptor.handleEventsWith(new OrderEventHandler());
      
      //3. 启动disruptor
      disruptor.start();
      
      //4. 获取实际存储数据的容器: RingBuffer
      RingBuffer<OrderEvent> ringBuffer = disruptor.getRingBuffer();
      
      OrderEventProducer producer = new OrderEventProducer(ringBuffer);
      
      ByteBuffer bb = ByteBuffer.allocate(8);
      
      for(long i = 0 ; i < 5; i ++){
         bb.putLong(0, i);
         producer.sendData(bb);
      }
      
      disruptor.shutdown();
      executor.shutdown();
      
   }
}

4、编写生产者组件:OrderEventProducer.java 

package com.bfxy.disruptor.quickstart;

import java.nio.ByteBuffer;

import com.lmax.disruptor.RingBuffer;

public class OrderEventProducer {

   private RingBuffer<OrderEvent> ringBuffer;
   
   public OrderEventProducer(RingBuffer<OrderEvent> ringBuffer) {
      this.ringBuffer = ringBuffer;
   }
   
   public void sendData(ByteBuffer data) {
      //1 在生产者发送消息的时候, 首先 需要从我们的ringBuffer里面 获取一个可用的序号
      long sequence = ringBuffer.next(); //0    
      try {
         //2 根据这个序号, 找到具体的 "OrderEvent" 元素 注意:此时获取的OrderEvent对象是一个没有被赋值的"空对象"
         OrderEvent event = ringBuffer.get(sequence);
         //3 进行实际的赋值处理
         event.setValue(data.getLong(0));         
      } finally {
         //4 提交发布操作
         ringBuffer.publish(sequence);        
      }
   }

 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!