@Override public SocketChannel newChannel(ChannelPipeline pipeline) { return lastChannel= super.newChannel(pipeline); } }
return super.newChannel(pipeline); } catch (Exception ex) { logger.error("Cannot create SSL channel", ex);
@Override public SocketChannel newChannel(ChannelPipeline pipeline) { try { SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[]{new BogusTrustManager()}, null); SSLEngine sslEngine = sslContext.createSSLEngine(); sslEngine.setUseClientMode(true); pipeline.addFirst("ssl", new SslHandler(sslEngine)); return super.newChannel(pipeline); } catch (Exception ex) { throw new RuntimeException("Cannot create SSL channel", ex); } } }
@Override public SocketChannel newChannel(ChannelPipeline pipeline) { try { ZlibEncoder encoder = new ZlibEncoder(6); pipeline.addFirst("deflater", encoder); pipeline.addFirst("inflater", new ZlibDecoder()); return super.newChannel(pipeline); } catch (Exception ex) { throw new RuntimeException("Cannot create Compression channel", ex); } } }
// Configure the client. final NioClientSocketChannelFactory clientSocketChannelFactory = new NioClientSocketChannelFactory( Executors.newCachedThreadPool()); // Set up the pipeline factory. final ChannelPipelineFactory channelPipelineFactory = new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline( new MyClientHandler()); } }; ChannelPipeline pipeline = channelPipelineFactory.getPipeline(); final Channel channel = clientSocketChannelFactory.newChannel(pipeline); channel.getConfig().setPipelineFactory(channelPipelineFactory); channel.getConfig().setOption("tcpNoDelay", true); channel.getConfig().setOption("receiveBufferSize", 1048576); channel.getConfig().setOption("sendBufferSize", 1048576); ChannelFuture boundFuture = Channels.future(channel); boundFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { future.getChannel().setAttachment(new Object());// set the channel attachment } } }); channel.getPipeline().sendDownstream(new DownstreamChannelStateEvent(channel, boundFuture, ChannelState.BOUND, new InetSocketAddress(host, 0))); ChannelFuture connectFuture = Channels.future(channel); channel.getPipeline().sendDownstream(new DownstreamChannelStateEvent(channel, connectFuture, ChannelState.CONNECTED, new InetSocketAddress(host, port))); channel.getCloseFuture().awaitUninterruptibly(); clientSocketChannelFactory.releaseExternalResources();// do not forget to do this
return super.newChannel(pipeline); } catch (Exception ex) { logger.error("Cannot create SSL channel", ex);