Java 类io.netty.channel.pool.AbstractChannelPoolHandler 实例源码

项目:RxS3    文件:NettyHttpClient.java   
public NettyHttpClient(ClientConfiguration configuration) {
    ThreadGroup threadGroup = new ThreadGroup("Netty RxS3 client");
    AtomicInteger threadCounter = new AtomicInteger();
    ThreadFactory threadFactory = r -> new Thread(threadGroup, r, "RxS3-client-worker" + threadCounter.getAndIncrement());
    group = new NioEventLoopGroup(configuration.getWorkerThreadCount(), threadFactory);

    String[] s3LocationArray = configuration.getS3Location().trim().split(":");

    s3Location = s3LocationArray[0];
    int port = 80;
    if (s3LocationArray.length == 2) {
        port = Integer.parseInt(s3LocationArray[1]);
    }

    demultiplexer = new HandlerDemultiplexer();

    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(group)
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.SO_KEEPALIVE, true)
            .option(ChannelOption.TCP_NODELAY, true)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, configuration.getConnectionTimeoutMillis())
            .channel(NioSocketChannel.class)
            .remoteAddress(s3Location, port);

    channelPool = new FixedChannelPool(bootstrap, new AbstractChannelPoolHandler() {

        HttpClientInitializer initializer = new HttpClientInitializer(demultiplexer);

        @Override
        public void channelCreated(Channel ch) {
            initializer.initChannel(ch);
        }
    }, ChannelHealthChecker.ACTIVE, FixedChannelPool.AcquireTimeoutAction.FAIL,
            configuration.getAcquireTimeoutMillis(), configuration.getMaxConnections(), configuration.getMaxPendingAcquires());
}