disruptor笔记2-核心链路的高级操作

disruptor 核心链路的高级操作

disruptor 控制核心链路十分方便,并且可以串行,并行,菱形,多边形链路操作。

  1. 启动类

    package com.tkn.disruptor.heigh.chain;
    
    import com.lmax.disruptor.BusySpinWaitStrategy;
    import com.lmax.disruptor.EventFactory;
    import com.lmax.disruptor.RingBuffer;
    import com.lmax.disruptor.dsl.Disruptor;
    import com.lmax.disruptor.dsl.EventHandlerGroup;
    import com.lmax.disruptor.dsl.ProducerType;
    
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class Main {
        public static void main(String[] args) throws InterruptedException {
    
            //构建一个线程池用于提交任务
            ExecutorService el = Executors.newFixedThreadPool(4);
    
            // 1 构建disruptor
            Disruptor<Trade> disruptor = new Disruptor<Trade>(
                    new EventFactory<Trade>() {
                        @Override
                        public Trade newInstance() {
                            return new Trade();
                        }
                    },
                    1024 * 1024,
                    Executors.defaultThreadFactory(),
                    ProducerType.SINGLE,
                    new BusySpinWaitStrategy()
            );
            //2 把消费者设置到Disruptor中 handleEventsWith
    
            //2.1 串行操作:
            disruptor.handleEventsWith(new Handler1())
                    .handleEventsWith(new Handler2())
                    .handleEventsWith(new Handler3());
    
            //2.2 并行操作: 可以有两种方式去进行
            //1 handleEventsWith方法 添加多个handler实现即可
            disruptor.handleEventsWith(new Handler1(),new Handler3(),new Handler2());
            //2 handleEventsWith方法 分别进行调用
            disruptor.handleEventsWith(new Handler1());
            disruptor.handleEventsWith(new Handler2());
            disruptor.handleEventsWith(new Handler3());
    
            //2.3 菱形操作 (一)
    
             disruptor.handleEventsWith(new Handler1(), new Handler2()).handleEventsWith(new Handler3());
            //2.3 菱形操作 (二)
            EventHandlerGroup<Trade> handlerGroup = disruptor.handleEventsWith(new Handler1(), new Handler2());
            handlerGroup.then(new Handler3());
    
            //2.4 六边形操作
    
            Handler1 h1 = new Handler1();
            Handler2 h2 = new Handler2();
            Handler3 h3 = new Handler3();
            Handler4 h4 = new Handler4();
            Handler5 h5 = new Handler5();
            disruptor.handleEventsWith(h1, h4);
            disruptor.after(h1).handleEventsWith(h2);
            disruptor.after(h4).handleEventsWith(h5);
            disruptor.after(h2,h5).handleEventsWith(h3);
    
            //3 启动disruptor
            RingBuffer<Trade> ringBuffer = disruptor.start();
    
            // 阻塞业务操作
            CountDownLatch countDownLatch = new CountDownLatch(1);
            long begin = System.currentTimeMillis();
            el.submit(new TradePushlisher(countDownLatch, disruptor));
    
            countDownLatch.await();//进行向下
            disruptor.shutdown();
            el.shutdown();
            System.err.println("总耗时: " + (System.currentTimeMillis() - begin));
    
    
        }
    }
    
    
  2. 实体类

    package com.tkn.disruptor.heigh.chain;
    
    import lombok.Data;
    
    import java.util.concurrent.atomic.AtomicInteger;
    @Data
    public class Trade {
        private String id;
        private String name;
        private double price;
        private AtomicInteger count = new AtomicInteger(0);
    }
    
    
  3. 生产者

    package com.tkn.disruptor.heigh.chain;
    
    import com.lmax.disruptor.EventTranslator;
    import com.lmax.disruptor.dsl.Disruptor;
    
    import java.util.Random;
    import java.util.concurrent.CountDownLatch;
    
    public class TradePushlisher implements Runnable {
        private CountDownLatch countDownLatch;
        private Disruptor<Trade> disruptor;
        private static int PUBLISH_COUNT = 1;
        public TradePushlisher(CountDownLatch countDownLatch, Disruptor<Trade> disruptor) {
            this.countDownLatch=countDownLatch;
            this.disruptor=disruptor;
        }
    
        @Override
        public void run() {
            for (int i = 0; i < PUBLISH_COUNT; i++) {
                //新的提交任务的方式
                disruptor.publishEvent(new EventTranslator<Trade>() {
                    private Random random = new Random();
                    @Override
                    public void translateTo(Trade trade, long l) {
                        trade.setPrice(random.nextDouble() * 9999);
                    }
                });
            }
            countDownLatch.countDown();
        }
    }
    
    
  4. 消费者1

    package com.tkn.disruptor.heigh.chain;
    
    import com.lmax.disruptor.EventHandler;
    import com.lmax.disruptor.WorkHandler;
    
    public class Handler1 implements EventHandler<Trade>, WorkHandler<Trade>{
    
    	//EventHandler
    	public void onEvent(Trade event, long sequence, boolean endOfBatch) throws Exception {
    		this.onEvent(event);
    	}
    
    	//WorkHandler
    	public void onEvent(Trade event) throws Exception {
    		System.err.println("handler 1 : SET NAME");
    		Thread.sleep(1000);
    		event.setName("H1");
    	}
    
    }
    
    
  5. 消费者2

    package com.tkn.disruptor.heigh.chain;
    
    import java.util.UUID;
    
    import com.lmax.disruptor.EventHandler;
    
    public class Handler2 implements EventHandler<Trade> {
    
    	public void onEvent(Trade event, long sequence, boolean endOfBatch) throws Exception {
    		System.err.println("handler 2 : SET ID");
    		Thread.sleep(2000);
    		event.setId(UUID.randomUUID().toString());
    	}
    
    }
    
    
  6. 消费者3

    package com.tkn.disruptor.heigh.chain;
    
    import com.lmax.disruptor.EventHandler;
    
    public class Handler3 implements EventHandler<Trade> {
    
    	public void onEvent(Trade event, long sequence, boolean endOfBatch) throws Exception {
    		System.err.println("handler 3 : NAME: " 
    								+ event.getName() 
    								+ ", ID: " 
    								+ event.getId()
    								+ ", PRICE: " 
    								+ event.getPrice()
    								+ " INSTANCE : " + event.toString());
    	}
    
    }
    
    
  7. 消费者4

    package com.tkn.disruptor.heigh.chain;
    
    import com.lmax.disruptor.EventHandler;
    
    public class Handler4 implements EventHandler<Trade> {
    
    	public void onEvent(Trade event, long sequence, boolean endOfBatch) throws Exception {
    		System.err.println("handler 4 : SET PRICE");
    		Thread.sleep(1000);
    		event.setPrice(17.0);
    	}
    
    }
    
    
  8. 消费者5

    package com.tkn.disruptor.heigh.chain;
    
    import com.lmax.disruptor.EventHandler;
    
    public class Handler5 implements EventHandler<Trade> {
    
    	public void onEvent(Trade event, long sequence, boolean endOfBatch) throws Exception {
    		System.err.println("handler 5 : GET PRICE: " +  event.getPrice());
    		Thread.sleep(1000);
    		event.setPrice(event.getPrice() + 3.0);
    	}
    
    }
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值