Example usage for io.netty.channel.pool AbstractChannelPoolMap AbstractChannelPoolMap

List of usage examples for io.netty.channel.pool AbstractChannelPoolMap AbstractChannelPoolMap

Introduction

In this page you can find the example usage for io.netty.channel.pool AbstractChannelPoolMap AbstractChannelPoolMap.

Prototype

AbstractChannelPoolMap

Source Link

Usage

From source file:com.heliosapm.streams.metrichub.HubManager.java

License:Apache License

private HubManager(final Properties properties) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            try {
                close();//from  w ww  .jav  a2 s.  com
            } catch (Exception x) {
                /* No Op */}
        }
    });
    log.info(">>>>> Initializing HubManager...");
    metricMetaService = new MetricsMetaAPIImpl(properties);
    tsdbEndpoint = TSDBEndpoint.getEndpoint(metricMetaService.getSqlWorker());
    for (String url : tsdbEndpoint.getUpServers()) {
        final URL tsdbUrl = URLHelper.toURL(url);
        tsdbAddresses.add(new InetSocketAddress(tsdbUrl.getHost(), tsdbUrl.getPort()));
    }
    endpointCount = tsdbAddresses.size();
    endpointSequence = new AtomicInteger(endpointCount);
    group = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2,
            metricMetaService.getForkJoinPool());
    bootstrap = new Bootstrap();
    bootstrap.handler(channelInitializer).group(group).channel(NioSocketChannel.class)
            .option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator())
            .option(ChannelOption.ALLOCATOR, BufferManager.getInstance());
    final ChannelPoolHandler poolHandler = this;
    poolMap = new AbstractChannelPoolMap<InetSocketAddress, SimpleChannelPool>() {
        @Override
        protected SimpleChannelPool newPool(final InetSocketAddress key) {
            final Bootstrap b = new Bootstrap().handler(channelInitializer).group(group).remoteAddress(key)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator())
                    .option(ChannelOption.ALLOCATOR, BufferManager.getInstance());
            return new SimpleChannelPool(b, poolHandler);
        }
    };
    eventExecutor = new DefaultEventExecutor(metricMetaService.getForkJoinPool());
    channelGroup = new DefaultChannelGroup("MetricHubChannelGroup", eventExecutor);

    //      tsdbAddresses.parallelStream().forEach(addr -> {
    //         final Set<Channel> channels = Collections.synchronizedSet(new HashSet<Channel>(3));
    //         IntStream.of(1,2,3).parallel().forEach(i -> {
    //            final ChannelPool pool = poolMap.get(addr); 
    //            try {channels.add(pool.acquire().awaitUninterruptibly().get());
    //            } catch (Exception e) {}
    //            log.info("Acquired [{}] Channels", channels.size());
    //            channels.parallelStream().forEach(ch -> pool.release(ch));
    //         });
    //      });

    log.info("<<<<< HubManager Initialized.");
}

From source file:org.eclipse.californium.elements.tcp.TcpClientConnector.java

License:Open Source License

@Override
public synchronized void start() throws IOException {
    if (rawDataChannel == null) {
        throw new IllegalStateException("Cannot start without message handler.");
    }/*www. j  ava 2s.c  om*/

    if (workerGroup != null) {
        throw new IllegalStateException("Connector already started");
    }

    workerGroup = new NioEventLoopGroup(numberOfThreads);
    poolMap = new AbstractChannelPoolMap<SocketAddress, ChannelPool>() {

        @Override
        protected ChannelPool newPool(SocketAddress key) {
            Bootstrap bootstrap = new Bootstrap().group(workerGroup).channel(NioSocketChannel.class)
                    .option(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.AUTO_READ, true)
                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeoutMillis).remoteAddress(key);

            // We multiplex over the same TCP connection, so don't acquire
            // more than one connection per endpoint.
            // TODO: But perhaps we could make it a configurable property.
            if (USE_FIXED_CONNECTION_POOL) {
                return new FixedChannelPool(bootstrap, new MyChannelPoolHandler(key), 1);
            } else {
                return new SimpleChannelPool(bootstrap, new MyChannelPoolHandler(key));
            }
        }
    };
}