Example usage for io.netty.channel ChannelFuture removeListener

List of usage examples for io.netty.channel ChannelFuture removeListener

Introduction

In this page you can find the example usage for io.netty.channel ChannelFuture removeListener.

Prototype

@Override
    ChannelFuture removeListener(GenericFutureListener<? extends Future<? super Void>> listener);

Source Link

Usage

From source file:com.farsunset.cim.sdk.android.CIMConnectorManager.java

License:Apache License

public void connect(final String host, final int port) {

    if (!isNetworkConnected(context)) {

        Intent intent = new Intent();
        intent.setAction(CIMConstant.IntentAction.ACTION_CONNECTION_FAILED);
        intent.putExtra(Exception.class.getName(), NetworkDisabledException.class.getSimpleName());
        context.sendBroadcast(intent);/*from  ww w . ja  v a 2s . c  o m*/

        return;
    }

    if (isConnected() || !semaphore.tryAcquire()) {
        return;
    }

    executor.execute(new Runnable() {
        @Override
        public void run() {

            final InetSocketAddress remoteAddress = new InetSocketAddress(host, port);
            bootstrap.connect(remoteAddress).addListener(new GenericFutureListener<ChannelFuture>() {

                @Override
                public void operationComplete(ChannelFuture future) {
                    semaphore.release();
                    future.removeListener(this);
                    if (!future.isSuccess() && future.cause() != null) {
                        handleConnectFailure(future.cause(), remoteAddress);
                    }

                    if (future.isSuccess()) {
                        channel = future.channel();
                    }

                }
            });
        }
    });

}

From source file:com.farsunset.cim.sdk.client.CIMConnectorManager.java

License:Apache License

public void connect(final String host, final int port) {

    if (isConnected() || !semaphore.tryAcquire()) {
        return;//from   ww  w  .j av a  2 s. c  o m
    }

    executor.execute(new Runnable() {
        @Override
        public void run() {

            logger.info("****************CIM?  " + host + ":" + port + "......");
            final InetSocketAddress remoteAddress = new InetSocketAddress(host, port);
            bootstrap.connect(remoteAddress).addListener(new GenericFutureListener<ChannelFuture>() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    semaphore.release();
                    future.removeListener(this);
                    if (!future.isSuccess() && future.cause() != null) {
                        handleConnectFailure(future.cause(), remoteAddress);
                    }

                    if (future.isSuccess()) {
                        channel = future.channel();
                    }
                }
            });
        }
    });

}

From source file:com.hipishare.chat.SecureChatClient.java

License:Apache License

public static void reConnect() {
    LOG.info("reConnect");
    ChannelFuture future = null;
    try {//  ww w.  java 2s.  co  m
        future = bootstrap.connect(HOST, PORT).sync();
        future.addListener(reConnectListener);
    } catch (Exception e) {
        e.printStackTrace();
        // future.addListener(channelFutureListener);
        future.removeListener(reConnectListener);
        LOG.info("");
    }
}

From source file:de.dfki.kiara.http.HttpHandler.java

License:Open Source License

public void closeChannel() {
    if (channel != null) {
        channel.closeFuture().addListener(new ChannelFutureListener() {

            @Override/*  w  w w . j av a  2  s.c  o m*/
            public void operationComplete(ChannelFuture future) throws Exception {
                future.removeListener(this);
                state = State.CLOSED;
                channel = null;
            }

        });
    }
}

From source file:org.acmsl.katas.antlr4netty.InterpreterServer.java

License:Open Source License

/**
 * Wraps given {@link ChannelFuture} to ensure the event loops
 * shut down gracefully.//from   w w  w.jav  a2  s  . c  o m
 * @param target the original channel future.
 * @param bossGroup the boss group.
 * @param workerGroup the worker group.
 * @return the wrapped future.
 */
@NotNull
protected ChannelFuture wrap(@NotNull final ChannelFuture target, @NotNull final NioEventLoopGroup bossGroup,
        @NotNull final NioEventLoopGroup workerGroup) {
    return new ChannelFuture() {
        @Override
        public Channel channel() {
            return target.channel();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ChannelFuture addListener(
                @NotNull final GenericFutureListener<? extends Future<? super Void>> listener) {
            return target.addListener(listener);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ChannelFuture addListeners(
                @NotNull final GenericFutureListener<? extends Future<? super Void>>... listeners) {
            return target.addListeners(listeners);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ChannelFuture removeListener(
                @NotNull final GenericFutureListener<? extends Future<? super Void>> listener) {
            return target.removeListener(listener);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ChannelFuture removeListeners(
                @NotNull final GenericFutureListener<? extends Future<? super Void>>... listeners) {
            return target.removeListeners(listeners);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ChannelFuture sync() throws InterruptedException {
            ChannelFuture result = null;

            try {
                result = target.sync();
            } finally {
                workerGroup.shutdownGracefully();
                bossGroup.shutdownGracefully();
            }

            return result;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ChannelFuture syncUninterruptibly() {
            return target.syncUninterruptibly();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ChannelFuture await() throws InterruptedException {
            return target.await();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ChannelFuture awaitUninterruptibly() {
            return target.awaitUninterruptibly();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isSuccess() {
            return target.isSuccess();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isCancellable() {
            return target.isCancellable();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Throwable cause() {
            return target.cause();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean await(final long timeout, @NotNull final TimeUnit unit) throws InterruptedException {
            return target.await(timeout, unit);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean await(final long timeoutMillis) throws InterruptedException {
            return target.await(timeoutMillis);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean awaitUninterruptibly(final long timeout, @NotNull final TimeUnit unit) {
            return target.awaitUninterruptibly(timeout, unit);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean awaitUninterruptibly(final long timeoutMillis) {
            return target.awaitUninterruptibly(timeoutMillis);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Void getNow() {
            return target.getNow();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean cancel(final boolean mayInterruptIfRunning) {
            return target.cancel(mayInterruptIfRunning);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isCancelled() {
            return target.isCancelled();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isDone() {
            return target.isDone();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Void get() throws InterruptedException, ExecutionException {
            return target.get();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Void get(final long timeout, @NotNull final TimeUnit unit)
                throws InterruptedException, ExecutionException, TimeoutException {
            return target.get(timeout, unit);
        }
    };
}

From source file:org.fiware.kiara.netty.BaseHandler.java

License:Open Source License

protected final void closeChannel() {
    if (channel != null) {
        channel.closeFuture().addListener(new ChannelFutureListener() {

            public void operationComplete(ChannelFuture future) throws Exception {
                future.removeListener(this);
                state = State.CLOSED;
                channel = null;/*from w  w  w .  j  a va  2 s .c om*/
            }

        });
    }
}