code: protected void connectCloseToChannelClose() {
          nettyChannel.closeFuture()
                      .addListener(new ChannelFutureListener() {
                          @Override
                          public void operationComplete(ChannelFuture future) throws Exception {
                              closeNow(); // Close this connection when the channel is closed.
                          }
                      });
          nettyChannel.attr(CONNECTION_ATTRIBUTE_KEY).set(this);
      }
docs: /*
  In order to make sure that the connection is correctly initialized, the listener needs to be added post
  constructor. Otherwise, there is a race-condition of the channel closed before the connection is completely
  created and the Connection.close() call on channel close can access the Connection object which isn't
  constructed completely. IOW, "this" escapes from the constructor if the listener is added in the constructor.