查看文档:http://netty.io/docs/stable/guide/html/。向下滚动至第9节。关闭应用程序。
你的主要应用AHS看起来是这样的:
package org.jboss.netty.example.time;
public class TimeServer {
static final ChannelGroup allChannels = new DefaultChannelGroup("time-server"(35));
public static void main(String[] args) throws Exception {
...
ChannelFactory factory = ...;
ServerBootstrap bootstrap = ...;
...
Channel channel = bootstrap.bind(...);
allChannels.add(channel);
...
// Shutdown code
ChannelGroupFuture future = allChannels.close();
future.awaitUninterruptibly();
factory.releaseExternalResources();
}
}
您处理器需要:
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) {
TimeServer.allChannels.add(e.getChannel());
}
你必须:1。 存储所有你在ChannelGroup
2.通道关闭时关闭所有频道并释放资源。
希望这会有所帮助。
我只是使用netty示例中给出的HttpSnoopServer(org.jboss.netty.example.http.snoop)。我在服务器处理程序中看到以下代码。 ChannelFuture future = e.getChannel()。write(response); //写入操作完成后关闭非保持连接。 if(!keepAlive) { future.addListener(ChannelFutureListener.CLOSE); }' 和你在那里指定的不一样吗? –