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?
Asked
Active
Viewed 1.5k times
28
-
This question might lead you to some information you haven't seen yet: http://stackoverflow.com/questions/6933347/how-should-one-use-disruptor-disruptor-pattern-to-build-real-world-message-sys – Jason Down Jan 14 '12 at 06:58
-
The [official project wiki](http://code.google.com/p/disruptor-net/w/list) has some information. – caesay Jan 14 '12 at 07:20
-
The one code example on their seems to be outdated - for instance, I can't find the interface "IBatchHandler" anywhere in the current build and the parameters of the RingBuffer seems to have entirely different parameters. It would seem that the official site has moved to GitHub but I can't find any additional examples/documentations on the site (but I'm not sure if I am doing it correctly) - https://github.com/odeheurles/Disruptor-net#readme – William Jan 14 '12 at 14:03
2 Answers
33
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);
}
}
}
}

Jonathan Oliver
- 5,207
- 32
- 31

paytools-steve
- 3,580
- 3
- 26
- 21
-
also can you add note how to run it? what dll should I add to a project? should I download them somewhere or I should build them myself? – Oleg Vazhnev Nov 07 '12 at 19:58
-
also can someone explaing an example? why it's better than standard .net libriries? how many threads are involved? just one? i don't see any threads creation. – Oleg Vazhnev Nov 07 '12 at 20:16
-
Most people using the disruptor pattern would likely be using a three stage pipeline, so it would be nice with an example with an Input Disruptor and an Output Disruptor. – Fred Jun 04 '14 at 12:55
-
1The comment for `_ringSize` is wrong. The value should be a power of 2, not a multiple of 2. – tzachs May 13 '16 at 21:07
3
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.

Peter Mortensen
- 30,738
- 21
- 105
- 131

Syed Waqas
- 2,576
- 4
- 29
- 36
-
1This link is stale. New link is http://www.tradesharp.se/over-6-million-transactions-per-second-in-a-real-time-system-an-out-of-the-box-approach/ – Simon Gillbee Nov 13 '15 at 22:36
-
-
2https://web.archive.org/web/20160726004920/http://tradesharp.se:80/over-6-million-transactions-per-second-in-a-real-time-system-an-out-of-the-box-approach/ – Anton Krouglov Jul 05 '18 at 15:25