disruptor基本用法

disruptor在高頻交易中有用到,現在用的的組件在log4j2中有用,作用如文中贅述:https://blog.csdn.net/weixin_36098377/article/details/82978273

簡介轉載於作者原文:https://www.cnblogs.com/snifferhu/p/7513856.html,有同事反饋比較吃cpu,現在還沒辦法解決,所以改回了kafka+自定義線程池的方式,有的還有用死信隊列+自定義線程池的方式維護消費者和生產者的關係的,還沒實踐

文中的demo如下:

public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {

        PersonHelper.start();
        for(int i=0 ; i<200; i++){
            Person p = new Person(i, i+"-" , 1);

            //生產者生產數據
            PersonHelper.produce(p);
        }
        //PersonHelper.shutdown();
    }
}
public class PersonHelper {

    private  static PersonHelper instance;
    /**
     * ringBuffer的容量,必須是2的N次方
     */
    private static final int BUFFER_SIZE = 2048;
    //用於處理數據結構跟蹤爲出版商和相關eventprocessors序列光標協調障礙
    private Disruptor<PersonEvent> disruptor;

    public PersonHelper(){

        ExecutorService executor = Executors.newCachedThreadPool();
        disruptor = new Disruptor<PersonEvent>(
                PersonEvent.EVENT_FACTORY,
                BUFFER_SIZE,
                executor,
                ProducerType.SINGLE,
                new YieldingWaitStrategy());

        EventHandler<PersonEvent> eventHandler = new PersonEventHandler();
        disruptor.handleEventsWith(eventHandler);
        disruptor.start();
    }

    /**
     * 啓動消費者線程,實際上調用了AudioDataEventHandler中的onEvent方法進行處理
     */
    public static void start(){
        instance = new PersonHelper();
    }
    /**
     * 停止
     */
    public static void shutdown(){
        instance.doHalt();
    }
    private void doHalt() {
        disruptor.halt();
    }

    /**
     * 生產者生產商品
     * @param person
     */
    private void doProduce(Person person){
        RingBuffer<PersonEvent> ringBuffer=disruptor.getRingBuffer();
        //獲取下一個序號
        long sequence = ringBuffer.next();
        //寫入數據
        disruptor.get(sequence).setPerson(person);
        //通知消費者該資源可以消費了
        System.out.println("第  "+sequence+"  個生產完了  "+"開始消費:"+new Date().toLocaleString());
        ringBuffer.publish(sequence);
    }
    /**
     * 生產者壓入生產數據
     * @param data
     */
    public static void produce(Person person){
        instance.doProduce(person);
    }

}
/**
 * 消費事件處理處理器
 */
public class PersonEventHandler  implements EventHandler<PersonEvent> {

    public PersonEventHandler(){

    }
    @Override
    public void onEvent(PersonEvent event, long sequence, boolean endOfBatch)
            throws Exception {

        Person person = event.getPerson();
        System.out.println("第 "+sequence+" 個消費結束:"+endOfBatch+"   "+new Date().toLocaleString());
    }

}
  <!-- https://mvnrepository.com/artifact/com.lmax/disruptor -->
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>3.4.2</version>
        </dependency>
/**
 * 消費事件
 */
public class PersonEvent {

    private Person person;

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
/*
 定義事件工廠*/
    public final static EventFactory<PersonEvent> EVENT_FACTORY = new EventFactory<PersonEvent>(){
        public PersonEvent newInstance(){
            return new PersonEvent();
        }
    };
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章