如何使用Java NIO有效地从套接字读取 [英] How to read efficiently from socket using Java NIO

查看:106
本文介绍了如何使用Java NIO有效地从套接字读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理从套接字交易报价中读取的任务,我需要实现最小延迟和高吞吐量。

I am working on task involving reading from the socket trading quotes and I need to achieve minimum latency and high throughput.

我开始使用最简单的可能的java nio原型像这样

I started with the simpliest possible java nio prototype like this

ByteBuffer buf = ByteBuffer.allocateDirect(BUFFER_SIZE);
try {
       buf.clear();
       int numBytesRead = socketChannel.read(buf);

       if (numBytesRead == -1) {  
           socketChannel.close();
       } else {
           buf.flip();
           byte[] byteArrived = new byte[buf.remaining];
           buf.get(byteArrived,0,byteArrived.length);
           // here we send byteArrived to the parser
       }
   } catch (IOException e) {    
}

我想每次创建byte []数组都很蹩脚,但由于缺乏知识,我不知道如何解析ByteBuffer(因为我需要将字节协议解组为消息并将它们传递给业务逻辑)。您能否建议如何避免大规模垃圾的创建?

I guess it is lame to create byte[] array every time, but due to the lack of knowledge I dont know how to parse ByteBuffer ( because I need to unmarshall byte protocol into messages and pass them into business logic). Can you recommend how to avoid mass garbage creation?

另外,我想问一下如何组织低延迟和高吞吐量的套接字读取的最佳实践?我读到了LMAX和disruptor框架,他们在单个线程上实现了6M交易。

Also I would like to ask about best practices how to organize socket reading with low latency and high throughput? I read about LMAX and disruptor framework and they achieved 6M transactions on the single thread.

推荐答案

你可以达到高于Disruptor的水平和其他方法。很大程度上取决于消息的大小和复杂性(以及你对消息的处理方式!!)

You can achieve higher than that with Disruptor and other methods. A lot depends on the size and complexity of message (as well as what you do with the message !!)

如果你想用ByteBuffer序列化/反序列化,请使用putXxxx和getXxxx方法。为了简化此过程,我建议先将每条消息的长度放在一起,这样您就可以在尝试解析之前检查是否有完整的消息。

If you want to serialize/deserialze with ByteBuffer, use the putXxxx and getXxxx methods. To make this process easier, I suggest putting the length of each message first so you can check you have a full message before attempting to parse it.

您可能会发现此演示文稿有趣的 http://vanillajava.blogspot.com/2011/11/low- latency-slides.html

You might find this presentation interesting http://vanillajava.blogspot.com/2011/11/low-latency-slides.html

这篇关于如何使用Java NIO有效地从套接字读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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