Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disruptor.NET example

Tags:

I am trying to learn how to use the Disruptor.NET messaging framework, and I can't find any practical examples. There are quite a few articles out there with pictures about how it works, but I can't find anywhere that actually goes and shows you how to implement the methods. What would be an example?

like image 755
William Avatar asked Jan 14 '12 06:01

William


People also ask

What is disruptor pattern?

The disruptor pattern is a batching queue backed up by a circular array (i.e. the ring buffer) filled with pre-allocated transfer objects which uses memory-barriers to synchronize producers and consumers through sequences.

What is LMAX Disruptor used for?

Disruptor is an open source Java library written by LMAX. It is a concurrent programming framework for the processing of a large number of transactions, with low-latency (and without the complexities of concurrent code).


2 Answers

Frustrated that I couldn't find a workable 'Hello World' for Disruptor-net, I fiddled around until I got one working - see below. Hopefully it's fairly self-explanatory. The Console.WriteLine lines are handy for seeing how things operate - for example, that the RingBuffer creates each entry instance at start-up (which makes sense).

Hope this helps anyone looking for help with Disruptor on .NET.

using System; using System.Linq; using System.Threading; using System.Threading.Tasks; using Disruptor; using Disruptor.Dsl;  namespace DisruptorTest {     public sealed class ValueEntry     {         public long Value { get; set; }          public ValueEntry()         {             Console.WriteLine("New ValueEntry created");         }     }      public class ValueAdditionHandler : IEventHandler<ValueEntry>     {         public void OnNext(ValueEntry data, long sequence, bool endOfBatch)         {             Console.WriteLine("Event handled: Value = {0} (processed event {1}", data.Value, sequence);         }     }      class Program     {         private static readonly Random _random = new Random();         private static readonly int _ringSize = 16;  // Must be multiple of 2          static void Main(string[] args)         {             var disruptor = new Disruptor.Dsl.Disruptor<ValueEntry>(() => new ValueEntry(), _ringSize, TaskScheduler.Default);              disruptor.HandleEventsWith(new ValueAdditionHandler());              var ringBuffer = disruptor.Start();              while (true)             {                 long sequenceNo = ringBuffer.Next();                  ValueEntry entry = ringBuffer[sequenceNo];                  entry.Value = _random.Next();                  ringBuffer.Publish(sequenceNo);                  Console.WriteLine("Published entry {0}, value {1}", sequenceNo, entry.Value);                  Thread.Sleep(250);             }         }     } } 
like image 131
SteveWilkinson Avatar answered Sep 18 '22 11:09

SteveWilkinson


There is a detailed blog post on the Disruptor pattern, The Latency Issue. It demonstrates how to get started and use the Disruptor in detail.

like image 25
Syed Waqas Avatar answered Sep 20 '22 11:09

Syed Waqas