Disruptor笔记(三)-处理模式

常见模式:

1.UniCast a series of items between 1 publisher and 1 EventProcessor:一个publisher 一个eventprocessor

Disruptor笔记(三)-处理模式_第1张图片

P1  - Publisher 1

RB  - RingBuffer

SB  - SequenceBarrier

EP1 -EventProcessor 1



代码:

[java]  view plain copy
  1. private final RingBuffer<ValueEvent> ringBuffer =  
  2.         new RingBuffer<ValueEvent>(ValueEvent.EVENT_FACTORY,  
  3.                                    new SingleThreadedClaimStrategy(BUFFER_SIZE),  
  4.                                    newYieldingWaitStrategy());  
  5.     private final SequenceBarrier sequenceBarrier = ringBuffer.newBarrier();  
  6.     private final ValueAdditionEventHandler handler = newValueAdditionEventHandler();  
  7.     private finalBatchEventProcessor<ValueEvent> batchEventProcessor = new BatchEventProcessor<ValueEvent>(ringBuffer, sequenceBarrier, handler);  
  8.     {  
  9.         ringBuffer.setGatingSequences(batchEventProcessor.getSequence());  
  10. }  


或 DSL写法

[java]  view plain copy
  1. Disruptor  disruptor = new Disruptor<TestEvent>(TestEvent.EVENT_FACTORY, executor,  
  2.                                              newSingleThreadedClaimStrategy(4),  
  3.                                              newBlockingWaitStrategy());  
  4. private final ValueAdditionEventHandler handler = new ValueAdditionEventHandler();  
  5. disruptor. handleEventsWith(handler);//  
  6. disruptor.start();  


2.Pipeline a series of messages

Disruptor笔记(三)-处理模式_第2张图片        

  P1  - Publisher 1

  RB  - RingBuffer

  SB1 - SequenceBarrier 1

  EP1 - EventProcessor 1

  SB2 - SequenceBarrier 2

  EP2 - EventProcessor 2

  SB3 - SequenceBarrier 3

  EP3 -EventProcessor 3

[java]  view plain copy
  1. private final RingBuffer<FunctionEvent> ringBuffer =  
  2.         new RingBuffer<FunctionEvent>(FunctionEvent.EVENT_FACTORY,  
  3.                                       new SingleThreadedClaimStrategy(BUFFER_SIZE),  
  4.                                       new YieldingWaitStrategy());  
  5.   
  6.     private final SequenceBarrier stepOneSequenceBarrier = ringBuffer.newBarrier();  
  7.     private final FunctionEventHandler stepOneFunctionHandler = new FunctionEventHandler(FunctionStep.ONE);  
  8.     private final BatchEventProcessor<FunctionEvent> stepOneBatchProcessor =  
  9.         new BatchEventProcessor<FunctionEvent>(ringBuffer, stepOneSequenceBarrier, stepOneFunctionHandler);  
  10.   
  11.   
  12.     private final SequenceBarrier stepTwoSequenceBarrier = ringBuffer.newBarrier(stepOneBatchProcessor.getSequence());  
  13.     private final FunctionEventHandler stepTwoFunctionHandler = new FunctionEventHandler(FunctionStep.TWO);  
  14.     private final BatchEventProcessor<FunctionEvent> stepTwoBatchProcessor =  
  15.         new BatchEventProcessor<FunctionEvent>(ringBuffer, stepTwoSequenceBarrier, stepTwoFunctionHandler);  
  16.   
  17.   
  18.     private final SequenceBarrier stepThreeSequenceBarrier = ringBuffer.newBarrier(stepTwoBatchProcessor.getSequence());  
  19.     private final FunctionEventHandler stepThreeFunctionHandler = new FunctionEventHandler(FunctionStep.THREE);  
  20.     private final BatchEventProcessor<FunctionEvent> stepThreeBatchProcessor =  
  21.         new BatchEventProcessor<FunctionEvent>(ringBuffer, stepThreeSequenceBarrier, stepThreeFunctionHandler);  
  22.     {  
  23.         ringBuffer.setGatingSequences(stepThreeBatchProcessor.getSequence());  
  24.     }  
[java]  view plain copy
  1.   
[java]  view plain copy
  1.   
[java]  view plain copy
  1. <pre name="code" class="java">Disruptor  disruptor = new Disruptor<TestEvent>(TestEvent.EVENT_FACTORY, executor,  
  2.                                              new SingleThreadedClaimStrategy(4),  
  3.                                              new BlockingWaitStrategy());  
  4. private final FunctionEventHandler stepOneFunctionHandler = new FunctionEventHandler(FunctionStep.ONE);  
  5. EventHandlerGroup< TestEvent > group = disruptor. handleEventsWith(stepOneFunctionHandler);  
  6.   
  7. private final FunctionEventHandler stepTwoFunctionHandler = new FunctionEventHandler(FunctionStep.TWO);  
  8.   
  9. private final FunctionEventHandler stepThreeFunctionHandler = new FunctionEventHandler(FunctionStep.THREE);  
  10. disruptor.handleEventsWith(stepOneFunctionHandler);  
  11. disruptor.after(stepOneFunctionHandler).handleEventsWith(stepTwoFunctionHandler);  
  12. disruptor.after(stepTwoFunctionHandler).handleEventsWith(stepThreeFunctionHandler);  
  13.   
  14. 或者  
  15. Disruptor. handleEventsWith(stepOneFunctionHandler).then(stepTwoFunctionHandler).then(stepThreeFunctionHandler);  
  16. disruptor.start();  
  17. </pre><br>  
  18. <br>  
  19. <pre></pre>  
  20. <p></p>  
  21. <p>3.<strong>Multicast a series of messages to multiple EventProcessors</strong></p>  
  22. <p><img src="http://hi.csdn.net/attachment/201203/8/0_1331177654ZLnL.gif" alt=""></p>  
  23. <p>P1  - Publisher 1</p>  
  24. <p align="left">RB  - RingBuffer</p>  
  25. <p align="left">SB  -SequenceBarrier</p>  
  26. <p align="left">EP1 - EventProcessor 1</p>  
  27. <p align="left">EP2 - EventProcessor 2</p>  
  28. <p>EP3 -EventProcessor 3</p>  
  29. <p></p>  
  30. <p></p>  
  31. <pre name="code" class="java">private final RingBuffer<ValueEvent> ringBuffer =  
  32.         new RingBuffer<ValueEvent>(ValueEvent.EVENT_FACTORY,  
  33.                                    new SingleThreadedClaimStrategy(BUFFER_SIZE),  
  34.                                    new YieldingWaitStrategy());  
  35.   
  36.     private final SequenceBarrier sequenceBarrier = ringBuffer.newBarrier();  
  37.   
  38.     private final ValueMutationEventHandler[] handlers = new ValueMutationEventHandler[NUM_EVENT_PROCESSORS];  
  39.     {  
  40.         handlers[0] = new ValueMutationEventHandler(Operation.ADDITION);  
  41.         handlers[1] = new ValueMutationEventHandler(Operation.SUBTRACTION);  
  42.         handlers[2] = new ValueMutationEventHandler(Operation.AND);  
  43.     }  
  44.   
  45.     private final BatchEventProcessor[] batchEventProcessors = new BatchEventProcessor[NUM_EVENT_PROCESSORS];  
  46.     {  
  47.         batchEventProcessors[0] = new BatchEventProcessor<ValueEvent>(ringBuffer, sequenceBarrier, handlers[0]);  
  48.         batchEventProcessors[1] = new BatchEventProcessor<ValueEvent>(ringBuffer, sequenceBarrier, handlers[1]);  
  49.         batchEventProcessors[2] = new BatchEventProcessor<ValueEvent>(ringBuffer, sequenceBarrier, handlers[2]);  
  50.   
  51.         ringBuffer.setGatingSequences(batchEventProcessors[0].getSequence(),  
  52.                                       batchEventProcessors[1].getSequence(),  
  53.                                       batchEventProcessors[2].getSequence());  
  54.     }<strong>  
  55. </strong></pre><pre name="code" class="java">Disruptor  disruptor = new Disruptor<TestEvent>(TestEvent.EVENT_FACTORY, executor,  
  56.                                              new SingleThreadedClaimStrategy(4),  
  57.                                              new BlockingWaitStrategy());  
  58. private final FunctionEventHandler stepOneFunctionHandler = new FunctionEventHandler(FunctionStep.ONE);  
  59. EventHandlerGroup< TestEvent > group = disruptor. handleEventsWith(stepOneFunctionHandler);  
  60.   
  61. private final FunctionEventHandler stepTwoFunctionHandler = new FunctionEventHandler(FunctionStep.TWO);  
  62.   
  63. private final FunctionEventHandler stepThreeFunctionHandler = new FunctionEventHandler(FunctionStep.THREE);  
  64.   
  65. disruptor. handleEventsWith(stepOneFunctionHandler ,stepTwoFunctionHandler,stepThreeFunctionHandler);  
  66. disruptor.start();  
  67. </pre><br>  
  68. 4.<strong>Replicate a message then fold back the results</strong>  
  69. <p></p>  
  70. <p><img src="http://hi.csdn.net/attachment/201203/8/0_1331177673MmKX.gif" alt=""><br>  
  71. </p>  
  72. <p> P1  - Publisher 1</p>  
  73. <p style="font-weight:bold"></p>  
  74. <p align="left"> RB  - RingBuffer</p>  
  75. <p align="left"> SB1 - SequenceBarrier 1</p>  
  76. <p align="left"> EP1 - EventProcessor 1</p>  
  77. <p align="left"> EP2 - EventProcessor 2</p>  
  78. <p align="left">SB2 - SequenceBarrier 2</p>  
  79. <p>EP3 -EventProcessor 3</p>  
  80. <pre name="code" class="java">private final RingBuffer<FizzBuzzEvent> ringBuffer =  
  81.         new RingBuffer<FizzBuzzEvent>(FizzBuzzEvent.EVENT_FACTORY,  
  82.                                       new SingleThreadedClaimStrategy(BUFFER_SIZE),  
  83.                                       new YieldingWaitStrategy());  
  84.   
  85.     private final SequenceBarrier sequenceBarrier = ringBuffer.newBarrier();  
  86.   
  87.     private final FizzBuzzEventHandler fizzHandler = new FizzBuzzEventHandler(FizzBuzzStep.FIZZ);  
  88.     private final BatchEventProcessor<FizzBuzzEvent> batchProcessorFizz =  
  89.         new BatchEventProcessor<FizzBuzzEvent>(ringBuffer, sequenceBarrier, fizzHandler);  
  90.   
  91.     private final FizzBuzzEventHandler buzzHandler = new FizzBuzzEventHandler(FizzBuzzStep.BUZZ);  
  92.     private final BatchEventProcessor<FizzBuzzEvent> batchProcessorBuzz =  
  93.         new BatchEventProcessor<FizzBuzzEvent>(ringBuffer, sequenceBarrier, buzzHandler);  
  94.   
  95.     private final SequenceBarrier sequenceBarrierFizzBuzz =  
  96.         ringBuffer.newBarrier(batchProcessorFizz.getSequence(), batchProcessorBuzz.getSequence());  
  97.   
  98.     private final FizzBuzzEventHandler fizzBuzzHandler = new FizzBuzzEventHandler(FizzBuzzStep.FIZZ_BUZZ);  
  99.     private final BatchEventProcessor<FizzBuzzEvent> batchProcessorFizzBuzz =  
  100.             new BatchEventProcessor<FizzBuzzEvent>(ringBuffer, sequenceBarrierFizzBuzz, fizzBuzzHandler);  
  101.     {  
  102.         ringBuffer.setGatingSequences(batchProcessorFizzBuzz.getSequence());  
  103. }  
  104. </pre><br>  
  105. <pre name="code" class="java">Disruptor  disruptor = new Disruptor<TestEvent>(TestEvent.EVENT_FACTORY, executor,  
  106.                                              new SingleThreadedClaimStrategy(4),  
  107.                                              new BlockingWaitStrategy());  
  108. private final FunctionEventHandler stepOneFunctionHandler = new FunctionEventHandler(FunctionStep.ONE);  
  109. EventHandlerGroup< TestEvent > group = disruptor. handleEventsWith(stepOneFunctionHandler);  
  110.   
  111. private final FunctionEventHandler stepTwoFunctionHandler = new FunctionEventHandler(FunctionStep.TWO);  
  112.   
  113. private final FunctionEventHandler stepThreeFunctionHandler = new FunctionEventHandler(FunctionStep.THREE);  
  114.   
  115. disruptor. handleEventsWith(stepOneFunctionHandler ,stepTwoFunctionHandler);  
  116. disruptor.after(stepOneFunctionHandler, stepTwoFunctionHandler).handleEventsWith(stepThreeFunctionHandler);  
  117. disruptor.start();  
  118. </pre><br>  
  119. 5.<strong>Sequence a series of messages from multiple publishers</strong>  
  120. <p></p>  
  121. <p></p>  
  122. <p style="font-weight:bold"><img src="http://hi.csdn.net/attachment/201203/8/0_13311776879is6.gif" alt=""><br>  
  123. </p>  
  124. <p style="font-weight:bold"> P1  - Publisher 1</p>  
  125.  P2  - Publisher 2<br>  
  126.  P3  - Publisher 3<br>  
  127.  RB  - RingBuffer<br>  
  128.  SB  - SequenceBarrier<br>  
  129.  EP1 - EventProcessor 1<br>  
  130. <pre name="code" class="java">private final RingBuffer<ValueEvent> ringBuffer =  
  131.         new RingBuffer<ValueEvent>(ValueEvent.EVENT_FACTORY,  
  132.                                    new MultiThreadedLowContentionClaimStrategy(BUFFER_SIZE),  
  133.                                    new YieldingWaitStrategy());  
  134.   
  135.     private final SequenceBarrier sequenceBarrier = ringBuffer.newBarrier();  
  136.     private final ValueAdditionEventHandler handler = new ValueAdditionEventHandler();  
  137.     private final BatchEventProcessor<ValueEvent> batchEventProcessor = new BatchEventProcessor<ValueEvent>(ringBuffer, sequenceBarrier, handler);  
  138.     private final ValuePublisher[] valuePublishers = new ValuePublisher[NUM_PUBLISHERS];  
  139.     {  
  140.         for (int i = 0; i < NUM_PUBLISHERS; i++)  
  141.         {  
  142.             valuePublishers[i] = new ValuePublisher(cyclicBarrier, ringBuffer, ITERATIONS / NUM_PUBLISHERS);  
  143.         }  
  144.   
  145.         ringBuffer.setGatingSequences(batchEventProcessor.getSequence());  
  146.     }</pre><br>  
  147. <br>  
  148. <p></p>  
  149. <p><strong><br>  
  150. </strong></p>  
  151. <br>  
  152. <pre></pre>  
  153. <pre></pre>  
  154. <pre></pre>  
  155. <pre></pre>  
  156. <pre></pre>  
  157. <pre></pre>  
  158. <pre></pre>  
  159. <pre></pre>  
  160. <pre></pre>  

你可能感兴趣的:(Disruptor笔记(三)-处理模式)