@Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt.equals(ClientHandshakeEvent.CONNECTED)) { // Handshake successful. Complete the connection future to allow // clients to proceed. channel = ctx.channel(); connectionFuture.complete(null); } else if (evt.equals(ClientHandshakeEvent.FAILED) && connectionFuture.isDone()) { // Handshake failed. If the current completion future is complete, // create a new one to unset it, causing future requests // to wait. connectionFuture = new CompletableFuture<>(); } else if (evt instanceof IdleStateEvent) { IdleStateEvent e = (IdleStateEvent) evt; if (e.state() == IdleState.READER_IDLE) { ctx.close(); } else if (e.state() == IdleState.WRITER_IDLE) { keepAlive(); } } else { log.warn("userEventTriggered: unhandled event {}", evt); } }