简体   繁体   中英

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. 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. I wonder if it's the same if I wrap my socket.getOutputStream around an 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.

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. 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. 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

Is that code not working in your case?

You said:

I am trying to figure out how to convert an object into a byte[]. Strings have the getBytes() method. 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. Am I understanding this correctly?

You are correct; wrapping the I/O streams with ObjectInputStream / ObjectOutputStream would again end up blocking things. The solution here might be to wrap a ByteArrayOutputStream in an ObjectOutputStream and write your objects to the underlying byte array. This underlying byte stream/array now has the byte representation (which follows the Java serialization specification obviously) of your object. From there on, it's the normal stuff with 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. The author recommends Apache Mina but I'll like to add another recommendation, "Jboss Netty". The author of Netty frequents SO so you can get your queries answered.

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. Much easier than mucking around with object streams etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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