使用 Java RealTime 的生产者-消费者架构
我正在使用 Java Realtime (Sun JRTS 2.2) 设计交易系统,并且想问一些有关最佳实践的问题,因为我担心发明轮子,并且非常确定我的任务已经解决了。
所以我有一个线程不断读取套接字,解析字节并提取消息(二进制协议)。之后,我应该向算法发送消息,该算法实际上会进行一些计算并决定是否进行交易。
所以我认为我应该设计这个系统的方法是将其分为两部分。生产者(周期性(?)实时线程从套接字中提取字节,解析它)和消费者(实时线程(周期性/零星?)从生产者中提取消息,对其进行操作等)。
所以第一个问题是如何设计这两个线程(生产者/消费者)之间的高性能通信?我也很想听听有关设计此类系统的现有经验、建议等的评论。
感谢您的帮助!
I am working on designing trade system using Java Realtime ( Sun JRTS 2.2 ) and would like to few questions about best practices, because I am afraid of inventing the wheel and pretty sure that my task was already solved.
So I have thread that continuously reading the socket, parsing bytes and extracting messages (binary protocol ). Afterwards, I should send messages to the algorithm, that actually does some calculation and make decision to trade or not.
So I think the way I should design this system is to split it into 2 parts. Producer ( periodic(?) Realtime thread that extracts bytes from socket, parse it ) and Consumer ( Realtime thread (periodic/sporadic?) that extracts messages from producer, manipulates with them etc ).
So the first question is how to design high performance communication between these 2 threads ( producer/consumer )? Also I would love to hear the comments about the existing experience in designing such systems, advices and etc.
Thank you for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在处理类似的问题,但在不同的领域:
以下是我的处理方式:
I am working with a similar problem but in a different domain:
Here is how I dealt with that:
在决定如何分解应用程序时,了解每个阶段需要多长时间以及可以并行完成哪些处理非常有用。您希望以微秒为单位计时每个阶段并测量分布。最有趣的点通常是 99%(最差的 1%)、99.9% 或 99.99% 区块延迟。
我想看看 disruptor 库。这是一个相当通用的库,专为高吞吐量和低延迟而设计。
如果您想要更简单的东西,有许多模式可以在亚微秒时间内在线程之间交换数据,但是这些都是点解决方案,具体取决于您的具体用例。
您可能会发现此演示很有趣,尽管它主要涉及测试低延迟、高吞吐量组件和通过套接字进行的通信。 http://vanillajava.blogspot.com/2011/11/low-latency -slides.html
When deciding how to break up your application it is useful to have a good idea of how long each stage takes and what processing can be done in parallel. You want to time each stage in micro-seconds and have measure the distribution. The most interesting points are usually the 99% (worst 1%), 99.9% or 99.99%tile latencies.
I would have a look at the disruptor library. This is a fairly general purpose library designed for high throughput and low latency.
If you want something simpler there is a number of patterns which can exchange data between threads in sub-microsecond time, however these are point solutions which depend on your specific use case.
You might find this presentation interesting, though it mostly deals with testing low latency, high throughput components and communication over sockets. http://vanillajava.blogspot.com/2011/11/low-latency-slides.html