Disruptor簡單樣例使用指南(非入門指南)

本文設定在讀者已經對Disruptor入門(至少是瞭解)的程度下的樣例使用。測試

我在剛看懂Disruptor的實現邏輯,而且學會使用Disruptor的簡單用例的時候,再去看它其中提供的其餘集中調用方式。好比OneToThreePipelineQueueThroughput等等,發現都是很固定的兩個,三個處理者,但一般一個業務流程,可能處理者有不少個。後來發現Disruptor是有then,and等執行方法的。這樣能夠本身設定hander處理順序,也就是用它最基礎的能力,來本身組裝handler的處理順序this

好比我如今有一個這樣的處理順序: 先處理A業務,根據A業務的結果處理B業務,再根據B業務的處理結果處理C業務。咱們將這個業務流程稱爲L流程(咱們樣例使用long數值來模擬)spa

那麼咱們這樣寫:對象

1.首先,寫幾個模擬A,B,C業務的處理代碼(各自的業務代碼),這裏value,twovalue,threevalue,模擬A,B,C三個步驟的業務傳遞數據。three

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;
    }
}ip

 

 

2。longEvent的工廠,用於建立L流程中傳遞的longevent對象(其實longevent能夠理解爲,當啓動一個L流程,流程管道中傳遞的數據流)。ci

public class LongEventFactory implements EventFactory<LongEvent> {

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

    }it

}io

 

 

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

相關文章
相關標籤/搜索