Disruptor简单样例使用指南(非入门指南)

本文设定在读者已经对Disruptor入门(至少是了解)的程度下的样例使用。

我在刚看懂Disruptor的实现逻辑,并且学会使用Disruptor的简单用例的时候,再去看它其中提供的其他集中调用方式。比如OneToThreePipelineQueueThroughput等等,发现都是很固定的两个,三个处理者,但通常一个业务流程,可能处理者有很多个。后来发现Disruptor是有then,and等执行方法的。这样可以自己设定hander处理顺序,也就是用它最基础的能力,来自己组装handler的处理顺序

比如我现在有一个这样的处理顺序: 先处理A业务,根据A业务的结果处理B业务,再根据B业务的处理结果处理C业务。我们将这个业务流程称为L流程(我们样例使用long数值来模拟)

那么我们这样写:

1.首先,写几个模拟A,B,C业务的处理代码(各自的业务代码),这里value,twovalue,threevalue,模拟A,B,C三个步骤的业务传递数据。

public class LongEvent {

    private long value;

    private long twoValue;

    private long threeValue;

    public long getValue() {
        return value;
    }

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

    public long getTwoValue() {
        return twoValue;
    }

    public void setTwoValue(long twoValue) {
        this.twoValue = twoValue;
    }

    public long getThreeValue() {
        return threeValue;
    }

    public void setThreeValue(long threeValue) {
        this.threeValue = threeValue;
    }
}

 

 

2。longEvent的工厂,用于创建L流程中传递的longevent对象(其实longevent可以理解为,当启动一个L流程,流程管道中传递的数据流)。

public class LongEventFactory implements EventFactory<LongEvent> {

    public LongEvent newInstance()
    {
        return new LongEvent();

    }

}

 

 

3.创建A,B,C三个业务的处理代码(模拟的),可以看到,我A处理时,会往B流程设置值,B处理时会往C流程设置值。这样便于观察A,B,C业务流程的处理顺序

public class OneEventHandler implements EventHandler<LongEvent> {

    public void onEvent(LongEvent event, long sequence, boolean endOfBatch)
    {
        event.setTwoValue(sequence);
        System.out.println("Event: " + event.getValue()+" sequence :"+sequence);
    }
}

 

public class ThreeEventHandler implements EventHandler<LongEvent> {

    public void onEvent(LongEvent event, long sequence, boolean endOfBatch)
    {
        System.out.println("Event three: " + event.getThreeValue()+" sequence :"+sequence);
    }
}

 

 

public class TwoEventHandler implements EventHandler<LongEvent> {

    public void onEvent(LongEvent event, long sequence, boolean endOfBatch)
    {
        event.setThreeValue(sequence);
        System.out.println("Event two:" + event.getTwoValue()+" sequence :"+sequence);
    }
}

 

4。测试代码(修改的官方样例代码),可以看到disruptor.handleEventsWith(new OneEventHandler()).then(new TwoEventHandler()).then(new ThreeEventHandler());,先执行one,再执行two,再执行three,也就是A执行完了,B再执行,然后再执行C。

public class MyDisruptorExample {

    public static void main(String[] args){
        // Executor that will be used to construct new threads for consumers
        Executor executor = Executors.newCachedThreadPool();

        // The factory for the event
        LongEventFactory factory = new LongEventFactory();

        // Specify the size of the ring buffer, must be power of 2.
        int bufferSize = 1024;

        // Construct the Disruptor
        Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(factorybufferSizeexecutor);

        // Connect the handler
        disruptor.handleEventsWith(new OneEventHandler()).then(new TwoEventHandler()).then(new ThreeEventHandler());

        // Start the Disruptor, starts all threads running
        disruptor.start();

        // Get the ring buffer from the Disruptor to be used for publishing.
        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();

        LongEventProducer producer = new LongEventProducer(ringBuffer);

        ByteBuffer bb = ByteBuffer.allocate(8);
        for (long l = 0; true; l++)
        {
            bb.putLong(0l);
            producer.onData(bb);
            try {
                Thread.sleep(100);
           catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 

 

5.测试结果:可以看到,全都是按照顺序,one,two,three执行的(具体逻辑可以看handler)。

Event: 0 sequence :0

Event two:0 sequence :0

Event three: 0 sequence :0

Event: 1 sequence :1

Event two:1 sequence :1

Event three: 1 sequence :1

Event: 2 sequence :2

Event two:2 sequence :2

Event three: 2 sequence :2

Event: 3 sequence :3

Event two:3 sequence :3

Event three: 3 sequence :3

Event: 4 sequence :4

Event two:4 sequence :4

Event three: 4 sequence :4

Event: 5 sequence :5

Event two:5 sequence :5

Event three: 5 sequence :5

Event: 6 sequence :6

Event two:6 sequence :6

Event three: 6 sequence :6

Event: 7 sequence :7

Event two:7 sequence :7

Event three: 7 sequence :7

Event: 8 sequence :8

Event two:8 sequence :8

Event three: 8 sequence :8

Event: 9 sequence :9

Event two:9 sequence :9

Event three: 9 sequence :9

Event: 10 sequence :10

Event two:10 sequence :10

Event three: 10 sequence :10

Event: 11 sequence :11

Event two:11 sequence :11

Event three: 11 sequence :11

Event: 12 sequence :12

Event two:12 sequence :12

Event three: 12 sequence :12

Event: 13 sequence :13

Event two:13 sequence :13

Event three: 13 sequence :13

Event: 14 sequence :14

Event two:14 sequence :14

Event three: 14 sequence :14

Event: 15 sequence :15

Event two:15 sequence :15

Event three: 15 sequence :15

Event: 16 sequence :16

Event two:16 sequence :16

Event three: 16 sequence :16

Event: 17 sequence :17

Event two:17 sequence :17

Event three: 17 sequence :17

Event: 18 sequence :18

Event two:18 sequence :18

Event three: 18 sequence :18

Event: 19 sequence :19

Event two:19 sequence :19

Event three: 19 sequence :19

Event: 20 sequence :20

Event two:20 sequence :20

Event three: 20 sequence :20

Event: 21 sequence :21

Event two:21 sequence :21

Event three: 21 sequence :21

Event: 22 sequence :22

Event two:22 sequence :22

Event three: 22 sequence :22

Event: 23 sequence :23

Event two:23 sequence :23

Event three: 23 sequence :23

Event: 24 sequence :24

Event two:24 sequence :24

Event three: 24 sequence :24

Event: 25 sequence :25

Event two:25 sequence :25

Event three: 25 sequence :25

Event: 26 sequence :26

Event two:26 sequence :26

Event three: 26 sequence :26

Event: 27 sequence :27

Event two:27 sequence :27

Event three: 27 sequence :27

Event: 28 sequence :28

Event two:28 sequence :28

Event three: 28 sequence :28

Event: 29 sequence :29

Event two:29 sequence :29

Event three: 29 sequence :29

Event: 30 sequence :30

Event two:30 sequence :30

Event three: 30 sequence :30

Event: 31 sequence :31

Event two:31 sequence :31

Event three: 31 sequence :31

Event: 32 sequence :32

Event two:32 sequence :32

Event three: 32 sequence :32

Event: 33 sequence :33

Event two:33 sequence :33

Event three: 33 sequence :33

Event: 34 sequence :34

Event two:34 sequence :34

Event three: 34 sequence :34

Event: 35 sequence :35

Event two:35 sequence :35

Event three: 35 sequence :35

Event: 36 sequence :36

Event two:36 sequence :36

Event three: 36 sequence :36

Event: 37 sequence :37

Event two:37 sequence :37

Event three: 37 sequence :37

Event: 38 sequence :38

Event two:38 sequence :38

Event three: 38 sequence :38

Event: 39 sequence :39

Event two:39 sequence :39

Event three: 39 sequence :39

 

 

6:我们修改下代码,现在将业务流程改成这样,同样是L流程,但是,A业务和B业务,可以同时执行增加处理速度,C业务必须等待A和B业务都结束了再执行。我们修改下测试代码:

 

disruptor.handleEventsWith(new OneEventHandler(),new TwoEventHandler()).then(new ThreeEventHandler());和上面唯一不同的就是这个handleEventsWith方法内,可以传不定数的handler。

public class MyDisruptorExample {

    public static void main(String[] args){
        // Executor that will be used to construct new threads for consumers
        Executor executor = Executors.newCachedThreadPool();

        // The factory for the event
        LongEventFactory factory = new LongEventFactory();

        // Specify the size of the ring buffer, must be power of 2.
        int bufferSize = 1024;

        // Construct the Disruptor
        Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(factorybufferSizeexecutor);

        // Connect the handler
        disruptor.handleEventsWith(new OneEventHandler(),new TwoEventHandler()).then(new ThreeEventHandler());

        // Start the Disruptor, starts all threads running
        disruptor.start();

        // Get the ring buffer from the Disruptor to be used for publishing.
        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();

        LongEventProducer producer = new LongEventProducer(ringBuffer);

        ByteBuffer bb = ByteBuffer.allocate(8);
        for (long l = 0; true; l++)
        {
            bb.putLong(0l);
            producer.onData(bb);
            try {
                Thread.sleep(100);
           catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 

 

7.我们看下执行结果,我标了两个红字,来区别下,可以看到two有时候为0,有时候不为0,这正是因为one和two的执行顺序是不定的,可能先two,可能先one,也有可能同时在执行,所以当one先执行,则two不会为0,但是当two先执行,或者one和two同时执行,则two因为没有通过one设值,所以为0

 

Event two:0 sequence :0

Event: 0 sequence :0

Event three: 0 sequence :0

Event two:0 sequence :1

Event: 1 sequence :1

Event three: 1 sequence :1

Event two:0 sequence :2

Event: 2 sequence :2

Event three: 2 sequence :2

Event two:0 sequence :3

Event: 3 sequence :3

Event three: 3 sequence :3

Event two:0 sequence :4

Event: 4 sequence :4

Event three: 4 sequence :4

Event two:0 sequence :5

Event: 5 sequence :5

Event three: 5 sequence :5

Event two:0 sequence :6

Event: 6 sequence :6

Event three: 6 sequence :6

Event two:0 sequence :7

Event: 7 sequence :7

Event three: 7 sequence :7

Event two:0 sequence :8

Event: 8 sequence :8

Event three: 8 sequence :8

Event two:0 sequence :9

Event: 9 sequence :9

Event three: 9 sequence :9

Event two:0 sequence :10

Event: 10 sequence :10

Event three: 10 sequence :10

Event two:0 sequence :11

Event: 11 sequence :11

Event three: 11 sequence :11

Event two:0 sequence :12

Event: 12 sequence :12

Event three: 12 sequence :12

Event two:0 sequence :13

Event: 13 sequence :13

Event three: 13 sequence :13

Event two:0 sequence :14

Event: 14 sequence :14

Event three: 14 sequence :14

Event two:0 sequence :15

Event: 15 sequence :15

Event three: 15 sequence :15

Event two:0 sequence :16

Event: 16 sequence :16

Event three: 16 sequence :16

Event two:0 sequence :17

Event: 17 sequence :17

Event three: 17 sequence :17

Event two:0 sequence :18

Event: 18 sequence :18

Event three: 18 sequence :18

Event two:0 sequence :19

Event: 19 sequence :19

Event three: 19 sequence :19

Event two:0 sequence :20

Event: 20 sequence :20

Event three: 20 sequence :20

Event two:0 sequence :21

Event: 21 sequence :21

Event three: 21 sequence :21

Event two:0 sequence :22

Event: 22 sequence :22

Event three: 22 sequence :22

Event two:0 sequence :23

Event: 23 sequence :23

Event three: 23 sequence :23

Event two:0 sequence :24

Event: 24 sequence :24

Event three: 24 sequence :24

Event two:0 sequence :25

Event: 25 sequence :25

Event three: 25 sequence :25

Event two:0 sequence :26

Event: 26 sequence :26

Event three: 26 sequence :26

Event two:0 sequence :27

Event: 27 sequence :27

Event three: 27 sequence :27

Event two:0 sequence :28

Event: 28 sequence :28

Event three: 28 sequence :28

Event two:0 sequence :29

Event: 29 sequence :29

Event three: 29 sequence :29

Event two:0 sequence :30

Event: 30 sequence :30

Event three: 30 sequence :30

Event two:0 sequence :31

Event: 31 sequence :31

Event three: 31 sequence :31

Event two:0 sequence :32

Event: 32 sequence :32

Event three: 32 sequence :32

Event two:0 sequence :33

Event: 33 sequence :33

Event three: 33 sequence :33

Event two:0 sequence :34

Event: 34 sequence :34

Event three: 34 sequence :34

Event: 35 sequence :35

Event two:35 sequence :35

Event three: 35 sequence :35

Event: 36 sequence :36

Event two:36 sequence :36

Event three: 36 sequence :36

Event: 37 sequence :37

Event two:37 sequence :37

Event three: 37 sequence :37

Event: 38 sequence :38

Event two:38 sequence :38

Event three: 38 sequence :38

Event: 39 sequence :39

Event two:39 sequence :39

Event three: 39 sequence :39

Event: 40 sequence :40

Event two:40 sequence :40

Event three: 40 sequence :40

Event: 41 sequence :41

Event two:41 sequence :41

Event three: 41 sequence :41

转载于:https://my.oschina.net/u/2525071/blog/826566

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值