1

チャネルを返す Netty 接続プールを開発する必要があるため、必要になるたびにチャネルを作成する必要がありません。クライアントは、HTTP 経由で 1 秒あたり数回のリクエストを行うことになっており、10 スレッドのグループから実行されます。現在、このメカニズムを回避する Netty チャネル プールを作成しましたが、接続していないようです。

(チャネルを生成しますが、チャネルはネットワーク上で「書き込み」を成功させることができません。

//Code follows
  //get Channel method
  public Channel getChannel() throws Exception 
  {
            synchronized (_channels) {
              PoolChannel pc = null;
              for (int i = 0; i < _channels.size(); i++) {
                pc = _channels.get(i);
                if (pc.lease()) {
                  // PoolConnection is available
                  if (!_checkChannels) {
                    return pc;
                  }
                  else {
                    // Check the status of the connection
                    boolean isHealthy = true;
                    try {
                      if (!pc.isOpen()) {
                    // If something wrong with the channel
                    // then don't use it anymore.
                        isHealthy = false;
                      }
                    }
                    catch(Exception ex) {
                      // If we can't even ask for that information, we
                      // certainly don't want to use it anymore.
                      isHealthy = false;
                    }

                    if (isHealthy) {
                      return pc;
                    }
                    else {
                      try {
                        pc.expire();
                      }
                      catch(Exception e) {
                        // ignore
                      }
                      _channels.remove(i);
                    }
                  }
                }
              }
            }

            // Create a new Connection
            PoolChannel pc=null;

            URI uri = _uri;
            String host = _host;
            int port = _port;

            //create channel
            Executor bossPool = Executors.newCachedThreadPool();
            Executor workerPool = Executors.newCachedThreadPool();

            ClientBootstrap bstrap = new ClientBootstrap(
                        new NioClientSocketChannelFactory(
                                bossPool, workerPool));
             bstrap.setPipelineFactory(new HttpClientPipelineFactory());

             ChannelFuture future = bstrap.connect(new InetSocketAddress(host, port));
             Channel chann = future.awaitUninterruptibly().getChannel();
             if (future.isSuccess()) {
                System.out.println("Channel success");
             }
            System.out.println("Channel = "+chann);
            pc = new PoolChannel(chann);
            pc.lease();

            // Add it to the pool
            synchronized (_channels) {
              _channels.add(pc);

              if (_cleaner == null) {
                // Put a new PoolCleaner
                _cleaner = new PoolCleaner(_cleaningInterval);
                _cleaner.start();
              }
            }

            return pc;
          }  

//===========================

これについてヘルプを得ることができますか? また、チャンネルを使い終わったので、このチャンネルをプールに戻すにはどうすればよいですか? チャネルを閉じるだけで十分ですか? 前もって感謝します。

4

0 に答える 0