@Override protected void initChannel(Channel channel) throws Exception { if (channel.pipeline().get("channelActivator") == null) { channel .pipeline() .addLast( "channelActivator", new RedisChannelInitializerImpl() { private AsyncCommand<?, ?, ?> pingCommand; @Override public Future<Boolean> channelInitialized() { return initializedFuture; } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { eventBus.publish(new DisconnectedEvent(local(ctx), remote(ctx))); initializedFuture = SettableFuture.create(); pingCommand = null; super.channelInactive(ctx); } @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof ConnectionEvents.Close) { if (ctx.channel().isOpen()) { ctx.channel().close(); } } if (evt instanceof ConnectionEvents.Activated) { if (!initializedFuture.isDone()) { initializedFuture.set(true); eventBus.publish(new ConnectionActivatedEvent(local(ctx), remote(ctx))); } } super.userEventTriggered(ctx, evt); } @Override public void channelActive(final ChannelHandlerContext ctx) throws Exception { eventBus.publish(new ConnectedEvent(local(ctx), remote(ctx))); if (pingBeforeActivate) { if (password != null && password.length != 0) { pingCommand = new AsyncCommand<>(INITIALIZING_CMD_BUILDER.auth(new String(password))); } else { pingCommand = new AsyncCommand<>(INITIALIZING_CMD_BUILDER.ping()); } pingBeforeActivate(pingCommand, initializedFuture, ctx, handlers); } else { super.channelActive(ctx); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (!initializedFuture.isDone()) { initializedFuture.setException(cause); } super.exceptionCaught(ctx, cause); } }); } for (ChannelHandler handler : handlers) { removeIfExists(channel.pipeline(), handler.getClass()); channel.pipeline().addLast(handler); } }
@Override protected void initChannel(Channel channel) throws Exception { SslContext sslContext; SSLParameters sslParams = new SSLParameters(); if (redisURI.isVerifyPeer()) { sslContext = SslContext.newClientContext(SslProvider.JDK); if (JavaRuntime.AT_LEAST_JDK_7) { sslParams.setEndpointIdentificationAlgorithm("HTTPS"); } } else { sslContext = SslContext.newClientContext(SslProvider.JDK, InsecureTrustManagerFactory.INSTANCE); } SSLEngine sslEngine = sslContext.newEngine(channel.alloc(), redisURI.getHost(), redisURI.getPort()); sslEngine.setSSLParameters(sslParams); removeIfExists(channel.pipeline(), SslHandler.class); if (channel.pipeline().get("first") == null) { channel .pipeline() .addFirst( "first", new ChannelDuplexHandler() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { eventBus.publish(new ConnectedEvent(local(ctx), remote(ctx))); super.channelActive(ctx); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { eventBus.publish(new DisconnectedEvent(local(ctx), remote(ctx))); super.channelInactive(ctx); } }); } SslHandler sslHandler = new SslHandler(sslEngine, redisURI.isStartTls()); channel.pipeline().addLast(sslHandler); if (channel.pipeline().get("channelActivator") == null) { channel .pipeline() .addLast( "channelActivator", new RedisChannelInitializerImpl() { private Command<?, ?, ?> pingCommand; @Override public Future<Boolean> channelInitialized() { return initializedFuture; } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { initializedFuture = SettableFuture.create(); pingCommand = null; super.channelInactive(ctx); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { if (initializedFuture.isDone()) { super.channelActive(ctx); } } @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof SslHandshakeCompletionEvent && !initializedFuture.isDone()) { SslHandshakeCompletionEvent event = (SslHandshakeCompletionEvent) evt; if (event.isSuccess()) { if (pingBeforeActivate) { pingCommand = INITIALIZING_CMD_BUILDER.ping(); pingBeforeActivate(pingCommand, initializedFuture, ctx, handlers); } else { ctx.fireChannelActive(); } } else { initializedFuture.setException(event.cause()); } } if (evt instanceof ConnectionEvents.Close) { if (ctx.channel().isOpen()) { ctx.channel().close(); } } if (evt instanceof ConnectionEvents.Activated) { if (!initializedFuture.isDone()) { initializedFuture.set(true); eventBus.publish(new ConnectionActivatedEvent(local(ctx), remote(ctx))); } } super.userEventTriggered(ctx, evt); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (!initializedFuture.isDone()) { initializedFuture.setException(cause); } super.exceptionCaught(ctx, cause); } }); } for (ChannelHandler handler : handlers) { removeIfExists(channel.pipeline(), handler.getClass()); channel.pipeline().addLast(handler); } }