一个人应该如何使用Disruptor(干扰者模式)来构建实际的消息系统? [英] How should one use Disruptor (Disruptor Pattern) to build real-world message systems?

查看:135
本文介绍了一个人应该如何使用Disruptor(干扰者模式)来构建实际的消息系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当RingBuffer预先分配给定类型的对象时,如何使用单个环形缓冲区来处理各种不同类型的消息?

As the RingBuffer up-front allocates objects of a given type, how can you use a single ring buffer to process messages of various different types?

您无法创建要插入到ringBuffer中的新对象实例,这会违背预先分配的目的.

You can't create new object instances to insert into the ringBuffer and that would defeat the purpose of up-front allocation.

因此您可能会以异步消息传递模式获得3条消息:

So you could have 3 messages in an async messaging pattern:

  1. NewOrderRequest
  2. NewOrderCreated
  3. NewOrderRejected

所以我的问题是,您打算如何在实际的消息系统中使用Disruptor模式?

So my question is how are you meant to use the Disruptor pattern for real-world messageing systems?

谢谢

链接: http://code.google.com/p/disruptor-net/wiki/CodeExamples

http://code.google.com/p/disruptor-net

http://code.google.com/p/disruptor

推荐答案

一种方法(我们最常见的模式)是将消息以其编组的形式(即字节数组)存储.对于传入的请求,例如修复消息(二进制消息)会迅速从网络中拉出并放置在环形缓冲区中.不同类型消息的解组和分发由该环形缓冲区上的EventProcessors(消费者)处理.对于出站请求,该消息被序列化为预先分配的字节数组,该数组形成环形缓冲区中的条目.

One approach (our most common pattern) is to store the message in its marshalled form, i.e. as a byte array. For incoming requests e.g. Fix messages, binary message, are quickly pulled of the network and placed in the ring buffer. The unmarshalling and dispatch of different types of messages are handled by EventProcessors (Consumers) on that ring buffer. For outbound requests, the message is serialised into the preallocated byte array that forms the entry in the ring buffer.

如果您使用某个固定大小的字节数组作为预分配条目,则需要一些其他逻辑来处理较大消息的溢出. IE.选择一个合理的默认大小,如果超出默认大小,则分配一个更大的临时数组.然后,当条目被重用或消费(取决于您的用例)并恢复为原始的预分配字节数组时,将其丢弃.

If you are using some fixed size byte array as the preallocated entry, some additional logic is required to handle overflow for larger messages. I.e. pick a reasonable default size and if it is exceeded allocate a temporary array that is bigger. Then discard it when the entry is reused or consumed (depending on your use case) reverting back to the original preallocated byte array.

如果您对不同消息类型有不同的使用者,则可以通过了解携带类型信息的字节数组中的偏移量或通过在条目上传递鉴别符值来快速确定您的使用者是否对特定消息感兴趣.

If you have different consumers for different message types you could quickly identify if your consumer is interested in the specific message either by knowing an offset into the byte array that carries the type information or by passing a discriminator value through on the entry.

也没有禁止创建对象实例和传递引用的规则(我们也在几个地方这样做).您确实失去了对象预分配的好处,但是破坏者的设计目标之一是允许用户选择最合适的存储形式.

Also there is no rule against creating object instances and passing references (we do this in a couple of places too). You do lose the benefits of object preallocation, however one of the design goals of the disruptor was to allow the user the choice of the most appropriate form of storage.

这篇关于一个人应该如何使用Disruptor(干扰者模式)来构建实际的消息系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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