2012-04-04 46 views
1

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

所以我有一些客户端类。这个类中创建的新版本:

  1. ChannelPipelineFactory(我的处理程序和webscoket客户handshaker)
  2. ClientBootstrap

在我有以下代码的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(); 
} 

但是,当停止()被调用的任何客户端将关闭所有的通道!?

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

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

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

+0

谢谢,似乎是一个问题。 – 2012-04-05 07:29:36