多个ClientBootstrap问题

时间:2012-04-04 13:07:00

标签: websocket netty

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

所以我有一些Client类。在这个课程中,我创建了新版本:

  1. ChannelPipelineFactory(使用我的处理程序和webscoket客户端握手)
  2. ClientBootstrap
  3. 在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
    

1 个答案:

答案 0 :(得分:1)

我认为您的问题是调用clientBootstrap.releaseExternalResources();

根据documentation ...此方法只需将调用委托给ChannelFactory.releaseExternalResources()。