io.netty.channel.pool.SimpleChannelPool Java Examples

The following examples show how to use io.netty.channel.pool.SimpleChannelPool. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BackClientPool.java    From api-gateway-core with Apache License 2.0 5 votes vote down vote up
private BackClientPool() {
    bootstrap
            .group(group)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 500)
            .channel(NioSocketChannel.class)
            .option(ChannelOption.TCP_NODELAY, true)
            .option(ChannelOption.SO_KEEPALIVE, true);

    poolMap = new AbstractChannelPoolMap<RequestHolder, SimpleChannelPool>() {
        @Override
        protected SimpleChannelPool newPool(RequestHolder requestHolder) {
            return new FixedChannelPool(bootstrap.remoteAddress(requestHolder.getSocketAddress()), new BackPoolHandler(requestHolder), 50);
        }
    };
}
 
Example #2
Source File: PoolTest.java    From util4j with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
		EventLoopGroup group = new NioEventLoopGroup();
		final Bootstrap cb = new Bootstrap();
		cb.group(group).channel(NioSocketChannel.class);
		InetSocketAddress addr1 = new InetSocketAddress("10.0.0.10", 8888);
		InetSocketAddress addr2 = new InetSocketAddress("10.0.0.11", 8888);

		//连接池map
		ChannelPoolMap<InetSocketAddress, SimpleChannelPool> poolMap = new AbstractChannelPoolMap<InetSocketAddress, SimpleChannelPool>() {
		    @Override
		    protected SimpleChannelPool newPool(InetSocketAddress key) {
		        return new SimpleChannelPool(cb.remoteAddress(key), new TestChannelPoolHandler());
		    }
		};

		final SimpleChannelPool pool1 = poolMap.get(addr1);//取出連接addr1地址的连接池
		final SimpleChannelPool pool2 = poolMap.get(addr2);//取出連接addr2地址的连接池
		Future<Channel> f1 = pool1.acquire();//获取一个连接
		f1.addListener(new FutureListener<Channel>() {
		    @Override
		    public void operationComplete(Future<Channel> f) {
		        if (f.isSuccess()) {
		            Channel ch = f.getNow();
		           //连接地址1的某个channel
		            //使用连接发送消息
//		            ch.write(msg)
		            //用完释放
		            pool1.release(ch);
		        }
		    }
		});

	}
 
Example #3
Source File: XioConnectionPool.java    From xio with Apache License 2.0 5 votes vote down vote up
public XioConnectionPool(Bootstrap bootstrap, AsyncRetryLoopFactory retryLoopFactory) {
  Preconditions.checkNotNull(bootstrap);
  this.retryLoopFactory = Preconditions.checkNotNull(retryLoopFactory);
  handler = bootstrap.config().handler();
  eventLoopGroup = bootstrap.config().group();
  simpleChannelPool = new SimpleChannelPool(bootstrap, channelPoolHandler, channelHealthChecker);
}
 
Example #4
Source File: Http2MultiplexedChannelPool.java    From ambry with Apache License 2.0 5 votes vote down vote up
/**
 * @param inetSocketAddress Remote Socket Address (IP address + port number).
 * @param sslFactory {@link SSLFactory} used for SSL connection.
 * @param eventLoopGroup The event loop group.
 * @param http2ClientConfig http2 client configs.
 * @param http2ClientMetrics http2 client metrics.
 * @param streamChannelInitializer {@link ChannelInitializer} to initilize a stream channel pipeline
 */
public Http2MultiplexedChannelPool(InetSocketAddress inetSocketAddress, SSLFactory sslFactory,
    EventLoopGroup eventLoopGroup, Http2ClientConfig http2ClientConfig, Http2ClientMetrics http2ClientMetrics,
    ChannelInitializer streamChannelInitializer) {
  this(new SimpleChannelPool(createBootStrap(eventLoopGroup, http2ClientConfig, inetSocketAddress),
          new Http2ChannelPoolHandler(sslFactory, inetSocketAddress.getHostName(), inetSocketAddress.getPort(),
              http2ClientConfig)), eventLoopGroup, ConcurrentHashMap.newKeySet(), inetSocketAddress, http2ClientConfig,
      http2ClientMetrics, streamChannelInitializer);
}
 
Example #5
Source File: NettyPooledTcpTransport.java    From async-gamequery-lib with MIT License 4 votes vote down vote up
@Override
public ChannelPool createChannelPool(InetSocketAddress key) {
    return new SimpleChannelPool(getBootstrap().remoteAddress(key), channelPoolHandler);
}