I'm planning to use Netty to design a TCP Server. When the client connects, I have to immediately start pumping <br>XML data to the client continuously...for hours/days. Its that simple.<br><br>So, I override "channelConnected" method and send data from that method, right?...thats great. <br>
<br><br>I will be using the following ChannelFactory<br> ChannelFactory factory =<br> new NioServerSocketChannelFactory(<br> Executors.newCachedThreadPool(),<br> Executors.newCachedThreadPool());<br>
<br>NioServerSocketChannelFactory documentation says -> "A worker thread performs non-blocking read and write for one or more Channels in a non-blocking mode." Good. <br><br>According to effective Java "Item 51: Don't depend on the thread scheduler", I want the worker thread to do a "unit of work" and then finish/return.<br>
<br>So in my case, though I have to send data continuously, I want to send some chunk (lets say 1 MB ) and then be done (unit of work completed), so that worker thread can return. Then I'll send another 1 MB.<br><br>
<br>Below example is from the official guide of Netty here -> <a href="http://docs.jboss.org/netty/3.2/guide/html_single/index.html#d0e440">http://docs.jboss.org/netty/3.2/guide/html_single/index.html#d0e440</a><br><br>
I guess the question is then, in this scenario, if i had to unconditionally keep sending time to the client, how would I do it, considering<br>each send as a unit of work. <br><br><br>One way of doing it would be to just put a while loop and do a Thread.Sleep. Any other way?<br>
<br><br><br> package org.jboss.netty.example.time;<br><br> public class TimeServerHandler extends SimpleChannelHandler {<br><br> @Override<br> public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {1<br>
Channel ch = e.getChannel();<br><br> ChannelBuffer time = ChannelBuffers.buffer(4);<br> time.writeInt(System.currentTimeMillis() / 1000);<br><br> ChannelFuture f = ch.write(time);<br>
<br> f.addListener(new ChannelFutureListener() {<br> public void operationComplete(ChannelFuture future) {<br> Channel ch = future.getChannel();<br> ch.close();<br>
}<br> });<br> }<br><br> @Override<br> public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {<br> e.getCause().printStackTrace();<br>
e.getChannel().close();<br> }<br> }<br><br>