我正在编写一个用于websocket服务器负载测试的工具。我需要创建大量(数万)到服务器的客户端连接。
所以我有一个客户端类。在这个类中,我创建了新版本的:
- ChannelPipelineFactory(与我的处理程序和webscocket客户端握手器)
- 客户端引导
在run()方法中,我有以下代码:
public void run() {
clientBootstrap.setPipelineFactory(clientChannelPipelineFactory);
ChannelFuture future = clientBootstrap.connect(
new InetSocketAddress(
clientConfiguration.getHost(),
clientConfiguration.getPort()
)
);
try {
future.awaitUninterruptibly().rethrowIfFailed();
WebSocketClientHandshaker handshaker = clientChannelPipelineFactory.getHandshaker();
channel = future.getChannel();
handshaker.handshake(channel).awaitUninterruptibly().rethrowIfFailed();
} catch (Exception e) {
log.error("Error in the client channel", e);
stop();
}
}
ChannelFuture返回的通道保存为客户端中的字段。
然后我做我的工作,并试图关闭所有打开的通道。stop()方法:
public void stop() {
log.debug(String.format("Close channel for client(%s)", id));
if (channel != null) {
if (channel.isWritable()) {
log.debug(String.format("Channel for client(%s) is writable", id));
ChannelFuture writeFuture = channel.write(new CloseWebSocketFrame());
writeFuture.addListener(ChannelFutureListener.CLOSE);
}
}
clientBootstrap.releaseExternalResources();
}
但是,当在任何客户端上调用stop()时,它会关闭所有通道!?
p.s。关闭所有通道的代码(单线程):
for (FSBBridgeServerClient client : clients) {
for (FSBBridgeServerClient subClient : clients) {
log.debug("c:" + subClient.getChannel());
log.debug("c:" + subClient.getChannel().isOpen());
}
client.stop();
}
一些调试日志:
2012-04-04 17:19:29,441 DEBUG [main] ClientApp - c:[id: 0x2344b18f, /127.0.0.1:38366 => localhost/127.0.0.1:5544]
2012-04-04 17:19:29,441 DEBUG [main] ClientApp - c:true
2012-04-04 17:19:29,442 DEBUG [main] ClientApp - c:[id: 0x01c20eb7, /127.0.0.1:38367 => localhost/127.0.0.1:5544]
2012-04-04 17:19:29,442 DEBUG [main] ClientApp - c:true
2012-04-04 17:19:34,414 DEBUG [main] ClientApp - c:[id: 0x2344b18f, /127.0.0.1:38366 :> localhost/127.0.0.1:5544]
2012-04-04 17:19:34,414 DEBUG [main] ClientApp - c:false
2012-04-04 17:19:34,414 DEBUG [main] ClientApp - c:[id: 0x01c20eb7, /127.0.0.1:38367 :> localhost/127.0.0.1:5544]
2012-04-04 17:19:34,414 DEBUG [main] ClientApp - c:false
我认为您的问题是调用clientBootstrap.releaseExternalResources();
。
根据文件。。。该方法只是将调用委托给ChannelFactory.releaseExternalResources().