List of usage examples for io.netty.channel.pool AbstractChannelPoolHandler AbstractChannelPoolHandler
AbstractChannelPoolHandler
From source file:com.splicemachine.olap.AsyncOlapNIOLayer.java
License:Apache License
public AsyncOlapNIOLayer(String host, int port) { InetSocketAddress socketAddr = new InetSocketAddress(host, port); Bootstrap bootstrap = new Bootstrap(); NioEventLoopGroup group = new NioEventLoopGroup(5, new ThreadFactoryBuilder().setNameFormat("olapClientWorker-%d").setDaemon(true) .setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { LOG.error("[" + t.getName() + "] Unexpected error in AsyncOlapNIO pool: ", e); }/* ww w . ja v a 2s .c om*/ }).build()); bootstrap.channel(NioSocketChannel.class).group(group).option(ChannelOption.SO_KEEPALIVE, true) .remoteAddress(socketAddr); //TODO -sf- this may be excessive network usage --consider a bounded pool to prevent over-connection? this.channelPool = new SimpleChannelPool(bootstrap, new AbstractChannelPoolHandler() { @Override public void channelCreated(Channel channel) throws Exception { ChannelPipeline p = channel.pipeline(); p.addLast("frameEncoder", new LengthFieldPrepender(4)); p.addLast("protobufEncoder", new ProtobufEncoder()); p.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1 << 30, 0, 4, 0, 4)); p.addLast("protobufDecoder", decoder); } }); executorService = group; }
From source file:org.eclipse.milo.opcua.stack.client.transport.http.OpcHttpTransport.java
License:Open Source License
private static ChannelPool createChannelPool(UaStackClientConfig config) { final String endpointUrl = config.getEndpoint().getEndpointUrl(); String host = EndpointUtil.getHost(endpointUrl); if (host == null) host = ""; int port = EndpointUtil.getPort(endpointUrl); LOGGER.debug("createChannelPool() host={} port={}", host, port); Bootstrap bootstrap = new Bootstrap().channelFactory(NioSocketChannel::new).group(config.getEventLoop()) .remoteAddress(host, port);//ww w .java 2s . co m return new SimpleChannelPool(bootstrap, new AbstractChannelPoolHandler() { @Override public void channelCreated(Channel channel) throws Exception { String scheme = EndpointUtil.getScheme(endpointUrl); if ("https".equalsIgnoreCase(scheme) || "opc.https".equalsIgnoreCase(scheme)) { SslContext sslContext = SslContextBuilder.forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE).build(); channel.pipeline().addLast(sslContext.newHandler(channel.alloc())); } int maxMessageSize = config.getMessageLimits().getMaxMessageSize(); channel.pipeline().addLast(new LoggingHandler(LogLevel.TRACE)); channel.pipeline().addLast(new HttpClientCodec()); channel.pipeline().addLast(new HttpObjectAggregator(maxMessageSize)); channel.pipeline().addLast(new OpcClientHttpCodec(config)); LOGGER.debug("channelCreated(): " + channel); } @Override public void channelAcquired(Channel channel) { LOGGER.debug("channelAcquired(): " + channel); } @Override public void channelReleased(Channel channel) { LOGGER.debug("channelReleased(): " + channel); } }); }