Disruptor.NET例子 [英] Disruptor.NET example

查看:600
本文介绍了Disruptor.NET例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何使用Disruptor.NET消息框架,我找不到任何实际的例子。有好几篇文章在那里与它是如何工作的照片,但我找不到任何地方,实际上去,并展示了如何实现的方法。这将是一个例子吗?

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?

推荐答案

沮丧,因为我无法找到一个可行的'Hello World'的干扰物网,我周围拨弄着,直到我得到了一个工作 - 见下文。希望这是相当不言自明。在 Console.WriteLine 行得心应手看到的东西如何运作 - 例如,该RingBuffer创建启动时的每个条目实例(这是有道理的)

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).

希望这可以帮助任何人寻找与.NET干扰器的帮助。

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

这篇关于Disruptor.NET例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆