查看文档: http: //netty.io/docs/stable/guide/html/。向下滚动到第 9 节。关闭您的应用程序。
您的主应用程序看起来像:
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. 关闭时,关闭所有频道并释放资源。
希望这可以帮助。