简体   繁体   English

如何在Java中将对象序列化为非阻塞套接字

[英]How can I serialize an object to a non-blocking socket in java

I have been reading about java nio and non-blocking sockets and I want to write serialized objects into the socket. 我一直在阅读有关Java nio和非阻塞套接字的信息,我想将序列化的对象写入套接字。 I was reading this article here http://www.owlmountain.com/tutorials/NonBlockingIo.htm#_Toc524339525 , and it says if you wrap your non-blocking socket around a PrintWriter, it will be blocking. 我在http://www.owlmountain.com/tutorials/NonBlockingIo.htm#_Toc524339525上阅读此文章,它说如果将非阻塞套接字包裹在PrintWriter周围,它将被阻塞。 I wonder if it's the same if I wrap my socket.getOutputStream around an ObjectOutputStream? 我想知道是否将我的socket.getOutputStream包装在ObjectOutputStream周围是否一样? Any easy way to test if a wrapper will be blocking or not? 有什么简单的方法可以测试包装是否被阻塞? I couldn't find any mentions of this in the PrintWriter or ObjectOutputStream documentation. 我在PrintWriter或ObjectOutputStream文档中找不到任何提及。

Here' a code snippet from the article above: 这是上面文章的代码片段:

else if ( key.isWritable() ) {

    Socket socket = (Socket) key.attachment();

    PrintWriter out = new PrintWriter( socket.getOutputStream(), true );

    out.println( "What is your name? " );
}

Not sure if you missed it or not, but the article clearly says than any conventional I/O utilities used won't cut in and goes out to present the sample code for reading and writing text from asynchronous channels. 不知道您是否错过了它,但是该文章清楚地表明,使用的任何常规I / O实用程序都不会插入并提供用于从异步通道读取和写入文本的示例代码。 Here is the relevant extract: 这是相关的摘录:

The problem with this code is that the PrintWriter blocks I/O and does not support the underlying asynchronous I/O mechanisms. 此代码的问题在于PrintWriter会阻止I / O,并且不支持底层的异步I / O机制。 To deal with this problem, we cannot use any of the standard I/O utilities, but instead must wrap our message in a ByteBuffer object and send it through the SocketChannel object 为了解决此问题,我们不能使用任何标准的I / O实用程序,而必须将消息包装在ByteBuffer对象中并通过SocketChannel对象发送

Is that code not working in your case? 该代码在您的情况下不起作用吗?

You said: 你说:

I am trying to figure out how to convert an object into a byte[]. 我试图弄清楚如何将一个对象转换为byte []。 Strings have the getBytes() method. 字符串具有getBytes()方法。 I am not sure what to use for a general serializable Object. 我不确定用于一般可序列化对象的内容。 I have been using ObjectOutput/InputStream classes but according to the article, if I use them, it will be blocking again. 我一直在使用ObjectOutput / InputStream类,但根据文章所述,如果使用它们,它将再次被阻塞。 Am I understanding this correctly? 我理解正确吗?

You are correct; 你是对的; wrapping the I/O streams with ObjectInputStream / ObjectOutputStream would again end up blocking things. ObjectInputStream / ObjectOutputStream包装I / O流将再次导致阻塞。 The solution here might be to wrap a ByteArrayOutputStream in an ObjectOutputStream and write your objects to the underlying byte array. 此处的解决方案可能是将ByteArrayOutputStream包装在ObjectOutputStream然后将您的对象写入基础字节数组。 This underlying byte stream/array now has the byte representation (which follows the Java serialization specification obviously) of your object. 现在,此基础字节流/数组具有对象的字节表示形式(显然遵循Java序列化规范)。 From there on, it's the normal stuff with NIO. 从那以后,这就是NIO的正常工作。 In case you are interested, there are some nice discussions here and here related to the thing I'm talking about. 如果您有兴趣, 这里这里有一些与我正在谈论的事情相关的不错的讨论。

EDIT: Also, I agree with the article's author that NIO is tricky to get right. 编辑:另外,我同意文章的作者,NIO很难正确。 The author recommends Apache Mina but I'll like to add another recommendation, "Jboss Netty". 作者推荐使用Apache Mina,但我还要添加另一条建议“ Jboss Netty”。 The author of Netty frequents SO so you can get your queries answered. Netty的作者经常这样做,因此您可以回答您的问题。

I'd also like to point out that if your motivation is to send across Java objects, use a framework which is tuned to those needs ie Java RMI or JBoss remoting. 我还要指出的是,如果您的动机是跨Java对象发送消息,请使用适合这些需求的框架,例如Java RMI或JBoss远程处理。 Much easier than mucking around with object streams etc. 比处理对象流等容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM