Example usage for io.netty.util.concurrent Future removeListener

List of usage examples for io.netty.util.concurrent Future removeListener

Introduction

In this page you can find the example usage for io.netty.util.concurrent Future removeListener.

Prototype

Future<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener);

Source Link

Document

Removes the first occurrence of the specified listener from this future.

Usage

From source file:org.opendaylight.netconf.client.TcpClientChannelInitializer.java

License:Open Source License

@Override
public void initialize(final Channel ch, final Promise<NetconfClientSession> promise) {
    final Future<NetconfClientSession> negotiationFuture = promise;

    //We have to add this channel outbound handler to channel pipeline, in order
    //to get notifications from netconf negotiatior. Set connection promise to
    //success only after successful negotiation.
    ch.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {
        ChannelPromise connectPromise;//ww  w  . ja  v  a2 s . co  m
        GenericFutureListener<Future<NetconfClientSession>> negotiationFutureListener;

        @Override
        public void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress,
                final SocketAddress localAddress, final ChannelPromise channelPromise) throws Exception {
            connectPromise = channelPromise;
            ChannelPromise tcpConnectFuture = new DefaultChannelPromise(ch);

            negotiationFutureListener = new GenericFutureListener<Future<NetconfClientSession>>() {
                @Override
                public void operationComplete(final Future<NetconfClientSession> future) throws Exception {
                    if (future.isSuccess()) {
                        connectPromise.setSuccess();
                    }
                }
            };

            tcpConnectFuture.addListener(new GenericFutureListener<Future<? super Void>>() {
                @Override
                public void operationComplete(final Future<? super Void> future) throws Exception {
                    if (future.isSuccess()) {
                        //complete connection promise with netconf negotiation future
                        negotiationFuture.addListener(negotiationFutureListener);
                    } else {
                        connectPromise.setFailure(future.cause());
                    }
                }
            });
            ctx.connect(remoteAddress, localAddress, tcpConnectFuture);
        }

        @Override
        public void disconnect(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception {
            // If we have already succeeded and the session was dropped after, we need to fire inactive to notify reconnect logic
            if (connectPromise.isSuccess()) {
                ctx.fireChannelInactive();
            }

            //If connection promise is not already set, it means negotiation failed
            //we must set connection promise to failure
            if (!connectPromise.isDone()) {
                connectPromise.setFailure(new IllegalStateException("Negotiation failed"));
            }

            //Remove listener from negotiation future, we don't want notifications
            //from negotiation anymore
            negotiationFuture.removeListener(negotiationFutureListener);

            super.disconnect(ctx, promise);
            promise.setSuccess();
        }
    });

    super.initialize(ch, promise);
}