You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by rdiegoc <rd...@gmail.com> on 2015/05/15 16:17:55 UTC

Read inputstream in Socket client for Netty Endpoint

Hi,

I am developing a module to replace an existing one. This new module is
based on camel.
This has a route that opens a socket consumer with netty4 like this:
*<route>
     <from
uri="netty4:tcp://localhost:7080?sync=true&amp;textline=true&amp;decoderMaxLineLength=10000"/>
            
     <to uri="direct:a"/>
</route>*

Then, I have a Java client that sends XMLs to socket for processing.
The java client was already written for the previous version of the module
and is necessary that the new module works with this client, because the
actual client (in production) is a hardware controller.
The client code is:
*client = new Socket("localhost", 7080);
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeBytes("test " + n + "\n");

byte[] buffer = new byte[8];
ByteArrayOutputStream w = new ByteArrayOutputStream();
int x = 0;

InputStream is = new BufferedInputStream(client.getInputStream());
try {
	while ((x = is.read(buffer)) > -1) {
		w.write(buffer, 0, x);
	}
} catch (SocketTimeoutException e) {
	// drop out to handle buffer
} catch (InterruptedIOException e) {
	// drop out to handle buffer
} catch (IOException e) {
	System.out.println("Read error:" + e);
}

System.out.println("Read: " + w.size() + "\n" + w.toString());
*
The client sends a XML and then waits for the response. The response is read
and saved as byte[] into a buffer, but when it reaches the end, the server
does not return -1 as the specification says:
/"If no byte is available Because the end of the stream Has Been Reached,
the value -1 is returned. This method blocks Until input data is available,
the end of the stream is detected, or an exception is thrown."/

Rather the java client is blocking.
When stop the JVM that is running the client, log in camel writes this:

2015-05-15 11:14:10,183 WARN [Camel (camel-1) thread #0 -
NettyEventExecutorGroup] NettyConsumer.warn(136) | Closing channel as an
exception was thrown from Netty. Caused by: [java.io.IOException - An
existing connection was forcibly closed by the remote host]
java.io.IOException: An existing connection was forcibly closed by the
remote host
	at sun.nio.ch.SocketDispatcher.read0(Native Method)
	at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
	at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
	at sun.nio.ch.IOUtil.read(IOUtil.java:192)
	at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:379)
	at
io.netty.buffer.UnpooledUnsafeDirectByteBuf.setBytes(UnpooledUnsafeDirectByteBuf.java:446)
	at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:881)
	at
io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:225)
	at
io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:119)
	at
io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
	at
io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
	at
io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
	at
io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
	at java.lang.Thread.run(Thread.java:745)


If the client reads the entire message using BufferedReader, this works
fine!!!
*client = new Socket("localhost", 7080);
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeBytes("MONOs " + n + "\n");

BufferedReader in = new BufferedReader(new
InputStreamReader(client.getInputStream()));

System.out.println(in.readLine());*

Then, is possible that the java client for camel-netty4 can read the
response using the first method?



--
View this message in context: http://camel.465427.n5.nabble.com/Read-inputstream-in-Socket-client-for-Netty-Endpoint-tp5767145.html
Sent from the Camel - Users mailing list archive at Nabble.com.