Ok. I'll let me ask the question a little differently - "How do I send data to the client continuously?". <br><br><br>In the code below I'm trying to send localtime every 5 secs to the client from within channelConnected method.<br>
But it doesn't work for me. It works only if I comment the while loop.<br>So if I want to send client time every 5 secs how do i do it?<br><br> public class SRNGServer {<br><br> public static void main(String[] args) throws Exception {<br>
// Configure the server.<br> ServerBootstrap bootstrap = new ServerBootstrap(<br> new NioServerSocketChannelFactory(<br> Executors.newCachedThreadPool(),<br>
Executors.newCachedThreadPool()));<br><br> // Configure the pipeline factory.<br> bootstrap.setPipelineFactory(new SRNGServerPipelineFactory());<br><br> // Bind and start to accept incoming connections.<br>
bootstrap.bind(new InetSocketAddress(8080));<br> }<br> }<br><br><br><br> public class SRNGServerHandler extends SimpleChannelUpstreamHandler {<br><br> private static final Logger logger = Logger.getLogger(<br>
SRNGServerHandler.class.getName());<br><br><br> @Override<br> public void channelConnected(<br> ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {<br>
// Send greeting for a new connection.<br> ChannelFuture writeFuture=e.getChannel().write("Welcome to " + InetAddress.getLocalHost().getHostName() + "!\r\n");<br><br><br> while(true){<br>
e.getChannel().write("It is " + new Date() + " now.\r\n");<br><br> Thread.sleep(1000*5);<br> }<br> }<br><br> @Override<br> public void exceptionCaught(<br>
ChannelHandlerContext ctx, ExceptionEvent e) {<br> logger.log(<br> Level.WARNING,<br> "Unexpected exception from downstream.",<br>
e.getCause());<br> e.getChannel().close();<br> }<br> }<br><br><br><br> public class SRNGServerPipelineFactory implements<br> ChannelPipelineFactory {<br>
<br> public ChannelPipeline getPipeline() throws Exception {<br> // Create a default pipeline implementation.<br> ChannelPipeline pipeline = pipeline();<br><br> pipeline.addLast("encoder", new StringEncoder());<br>
pipeline.addLast("handler", new SRNGServerHandler());<br><br> return pipeline;<br> }<br> }<br><br><br><div class="gmail_quote">On Fri, Jul 22, 2011 at 2:07 AM, Ioan Eugen Stan <span dir="ltr"><<a href="mailto:stan.ieugen@gmail.com">stan.ieugen@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">2011/7/22 Amit Phatarphekar <<a href="mailto:amitechforums@gmail.com">amitechforums@gmail.com</a>>:<br>
<div><div></div><div class="h5">> I'm planning to use Netty to design a TCP Server. When the client connects,<br>
> 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,<br>
> 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<br>
> performs non-blocking read and write for one or more Channels in a<br>
> non-blocking mode." Good.<br>
><br>
> According to effective Java "Item 51: Don't depend on the thread scheduler",<br>
> 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<br>
> chunk (lets say 1 MB ) and then be done (unit of work completed), so that<br>
> worker thread can return. Then I'll send another 1 MB.<br>
><br>
><br>
> Below example is from the official guide of Netty here -><br>
> <a href="http://docs.jboss.org/netty/3.2/guide/html_single/index.html#d0e440" target="_blank">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<br>
> 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<br>
> 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,<br>
> 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,<br>
> ExceptionEvent e) {<br>
> e.getCause().printStackTrace();<br>
> e.getChannel().close();<br>
> }<br>
> }<br>
><br>
><br>
</div></div>> _______________________________________________<br>
> netty-users mailing list<br>
> <a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
> <a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
><br>
<br>
I didn't understand what exactly is the question, but from what I know<br>
you should have no problems sending data continuously for long periods<br>
of time. And the amount you mentioned is not that large if we are<br>
talking about Ethernet speeds. You should give it a try and see what<br>
happens. If you run into trouble, seek help.<br>
<br>
I don't think the sleep is necessary. Reading and writing in Netty is<br>
done when there is data available and when it's room in the buffer.<br>
<br>
Hope that helps,<br>
<font color="#888888">--<br>
Ioan Eugen Stan<br>
<a href="http://ieugen.blogspot.com/" target="_blank">http://ieugen.blogspot.com/</a><br>
<br>
_______________________________________________<br>
netty-users mailing list<br>
<a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a></font></blockquote></div><br>