I m planning to use Netty to design a TCP Server. When the client connects, I have to immediately start pumping XML data to the client continuously...for hours/days. Its that simple.
因此,我凌驾于“渠道”方法之上,并从这一方法中发送数据,是正确的。
我将使用以下渠道。
ChannelFactory factory =
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool());
<>NioServerSocketChannelFactory
工人读书以非锁定的方式为一个或多个频道读写。
良好。
根据有效的Java 项目51:Don t 取决于校对进度表,我希望工人准备做“工作单位”,然后完成/恢复。
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.
Below example is from the official guide of Netty HERE.
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
each send as a unit of work.
这样做的一个途径是,只 put一.,做一read。 睡觉。 任何其他方式?
package org.jboss.netty.example.time;
public class TimeServerHandler extends SimpleChannelHandler {
@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
Channel ch = e.getChannel();
ChannelBuffer time = ChannelBuffers.buffer(4);
time.writeInt(System.currentTimeMillis() / 1000);
ChannelFuture f = ch.write(time);
f.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) {
Channel ch = future.getChannel();
ch.close();
}
});
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
e.getCause().printStackTrace();
e.getChannel().close();
}
}