2011-03-30 102 views
2

我試圖測試netty,但是當我創建多個客戶端連接到服務器時,一些客戶端只是凍結,永遠不會完成。 這裏我的代碼(基本上,我使用的代碼從她https://github.com/brunodecarvalho/netty-tutorials,只是修改它使用幾個客戶端):Netty與多個客戶端連接凍結


for (int i = numthr; i > 0; i--) { 
      Runnable runner = new Runnable() { 
       public void run() { 
        final Client client = new Client("localhost", 10400, nummes, 0); 
        if (!client.start()) { 
         System.exit(-1); 
         return; 
        } 
        client.flood(); 
        Runtime.getRuntime().addShutdownHook(new Thread() { 
         @Override 
         public void run() { 
          client.stop(); 
         } 
        }); 
       } 
      }; 
      executor.execute(runner); 
     } 

    public void messageReceived(Envelope message) { 

     if (this.received.incrementAndGet() == this.messages) { 

      System.out.println(nmb.incrementAndGet()); 


     } 
    } 

    public boolean start() { 

     // For production scenarios, use limited sized thread pools 
     this.clientFactory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), 
                   Executors.newCachedThreadPool(),1); 
     this.channelGroup = new DefaultChannelGroup(this + "-channelGroup"); 
     this.handler = new ClientHandler(this, this.channelGroup); 
     ChannelPipelineFactory pipelineFactory = new ChannelPipelineFactory() { 

      @Override 
      public ChannelPipeline getPipeline() throws Exception { 
       ChannelPipeline pipeline = Channels.pipeline(); 
       pipeline.addLast("encoder", new Encoder()); 
       pipeline.addLast("decoder", new Decoder()); 
       pipeline.addLast("handler", handler); 
       return pipeline; 
      } 
     }; 

     ClientBootstrap bootstrap = new ClientBootstrap(this.clientFactory); 
     bootstrap.setOption("reuseAddress", true); 
     bootstrap.setOption("tcpNoDelay", true); 
     bootstrap.setOption("keepAlive", true); 
     bootstrap.setPipelineFactory(pipelineFactory); 


     boolean connected = bootstrap.connect(new InetSocketAddress(host, port)).awaitUninterruptibly().isSuccess(); 
     if (!connected) { 
      this.stop(); 
     } 

     return connected; 
    } 

     this.serverFactory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), 
                   Executors.newCachedThreadPool()); 
     this.channelGroup = new DefaultChannelGroup(this + "-channelGroup"); 
     ExecutionHandler executionHandler = new ExecutionHandler(
       new MemoryAwareThreadPoolExecutor(270, 1048576, 1048576)); 

     ServerBootstrap bootstrap = new ServerBootstrap(this.serverFactory); 
     bootstrap.setPipelineFactory(
       new DatabaseGatewayPipelineFactory(executionHandler)); 
     bootstrap.setOption("reuseAddress", true); 
     bootstrap.setOption("child.tcpNoDelay", true); 
     bootstrap.setOption("child.keepAlive", true); 
     bootstrap.setOption("child.connectTimeoutMillis", 10000); 

     Channel channel = bootstrap.bind(new InetSocketAddress(this.host, this.port)); 
     if (!channel.isBound()) { 
      this.stop(); 
      return false; 
     } 

     this.channelGroup.add(channel); 

回答

1

代碼每次都爲Client創建新線程,具有相同的端口號。這可能會產生問題,因爲多個線程正在同一端口上處理消息。

-1

調用System.exit(-1);將終止JVM,而其他客戶端線程可能仍處於活動狀態。這種行爲是否有必要?

+0

我會說它沒關係。只有在客戶端沒有啓動時發生錯誤的情況下,該行纔會運行。它並沒有運行 – Aldarund 2011-07-19 19:08:45