Example usage for io.netty.channel ChannelFuture isCancellable

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

Introduction

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

Prototype

boolean isCancellable();

Source Link

Document

returns true if and only if the operation can be cancelled via #cancel(boolean) .

Usage

From source file:com.lambdaworks.redis.protocol.ReconnectionHandler.java

License:Apache License

protected boolean reconnect(InternalLogLevel infoLevel) throws Exception {

    SocketAddress remoteAddress = socketAddressSupplier.get();

    try {//  w  w w. j  a va 2s  . c o  m
        long timeLeft = timeoutUnit.toNanos(timeout);
        long start = System.nanoTime();

        logger.debug("Reconnecting to Redis at {}", remoteAddress);
        ChannelFuture currentFuture = this.currentFuture = bootstrap.connect(remoteAddress);
        if (!currentFuture.await(timeLeft, TimeUnit.NANOSECONDS)) {
            if (currentFuture.isCancellable()) {
                currentFuture.cancel(true);
            }

            throw new TimeoutException(
                    "Reconnection attempt exceeded timeout of " + timeout + " " + timeoutUnit);
        }

        currentFuture.sync();

        Channel channel = currentFuture.channel();

        RedisChannelInitializer channelInitializer = channel.pipeline().get(RedisChannelInitializer.class);
        CommandHandler<?, ?> commandHandler = channel.pipeline().get(CommandHandler.class);

        if (channelInitializer == null) {
            logger.warn("Reconnection attempt without a RedisChannelInitializer in the channel pipeline");
            close(channel);
            return false;
        }

        if (commandHandler == null) {
            logger.warn("Reconnection attempt without a CommandHandler in the channel pipeline");
            close(channel);
            return false;
        }

        try {
            timeLeft -= System.nanoTime() - start;
            channelInitializer.channelInitialized().get(Math.max(0, timeLeft), TimeUnit.NANOSECONDS);
            if (logger.isDebugEnabled()) {
                logger.log(infoLevel, "Reconnected to {}, Channel {}", remoteAddress,
                        ChannelLogDescriptor.logDescriptor(channel));
            } else {
                logger.log(infoLevel, "Reconnected to {}", remoteAddress);
            }
            return true;
        } catch (TimeoutException e) {
            channelInitializer.channelInitialized().cancel(true);
        } catch (Exception e) {
            if (clientOptions.isCancelCommandsOnReconnectFailure()) {
                commandHandler.reset();
            }

            if (clientOptions.isSuspendReconnectOnProtocolFailure()) {
                logger.error("Cannot initialize channel. Disabling autoReconnect", e);
                setReconnectSuspended(true);
            } else {
                logger.error("Cannot initialize channel.", e);
                throw e;
            }
        }
    } finally {
        this.currentFuture = null;
    }

    return false;
}

From source file:com.streamsets.pipeline.lib.network.BaseNettyServer.java

License:Apache License

public void destroy() {
    LOG.info("Destroying server on address(es) {}", addresses);
    for (ChannelFuture channelFuture : channelFutures) {
        if (channelFuture != null && channelFuture.isCancellable()) {
            channelFuture.cancel(true);/*from  ww  w  . j  a v  a2  s.  c om*/
        }
    }
    try {
        for (EventLoopGroup group : groups) {
            if (group != null && !group.isShutdown() && !group.isShuttingDown()) {
                try {
                    group.shutdownGracefully().get();
                } catch (InterruptedException ex) {
                    LOG.error("InterruptedException thrown while shutting down: " + ex, ex);
                    Thread.currentThread().interrupt();
                } catch (Exception ex) {
                    LOG.error("Unexpected error shutting down: " + ex, ex);
                }
            }
        }
    } finally {
        channelFutures.clear();
    }
}

From source file:com.streamsets.pipeline.stage.origin.udp.UDPConsumingServer.java

License:Apache License

public void destroy() {
    LOG.info("Destorying server on address(es) {}", addresses);
    for (ChannelFuture channelFuture : channelFutures) {
        if (channelFuture != null && channelFuture.isCancellable()) {
            channelFuture.cancel(true);// w w  w .  ja  v a 2  s  .c  o m
        }
    }
    if (group != null && !group.isShutdown() && !group.isShuttingDown()) {
        try {
            group.shutdownGracefully().get();
        } catch (InterruptedException ex) {
            // ignore
        } catch (Exception ex) {
            LOG.error("Unexpected error shutting down: " + ex, ex);
        }
    }
    group = null;
    channelFutures.clear();
}

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 ww  .j  a  va 2  s . c om*/
 * @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);
        }
    };
}