Disruptor.NET 示例

发布于 2024-12-26 17:55:42 字数 103 浏览 0 评论 0原文

我正在尝试学习如何使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

油焖大侠 2025-01-02 17:55:42

由于无法为 Disruptor-net 找到一个可行的“Hello World”,我感到很沮丧,所以我一直在摆弄,直到找到一个可以工作的。见下文。 Console.WriteLine 行可以方便地查看事情是如何工作的。例如,RingBuffer 在启动时创建每个条目实例(这是有道理的)。

希望这可以帮助任何在 .NET 上寻求 Disruptor 帮助的人。

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

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

Hopefully 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 power 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);
            }
        }
    }
}
浮世清欢 2025-01-02 17:55:42

有一篇关于 Disruptor 模式的详细博客文章,延迟问题。它详细演示了如何开始和使用 Disruptor。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文