1

我正在编写一个用于 websocket 服务器负载测试的工具。我需要创建很多(数万个)到服务器的客户端连接。

所以我有一个客户端类。在这个类中,我创建了以下新版本:

  1. ChannelPipelineFactory(与我的处理程序和 webscoket 客户端握手)
  2. 客户端引导

在 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() 时,它会关闭所有通道!?

ps 关闭所有通道的代码(单线程):

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
4

1 回答 1

1

我认为你的问题是打电话clientBootstrap.releaseExternalResources();

根据文档...此方法只是将调用委托给 ChannelFactory.releaseExternalResources()。

于 2012-04-04T23:04:16.983 回答