Java Code Examples for io.netty.channel.ChannelFuture#isCancelled()

The following examples show how to use io.netty.channel.ChannelFuture#isCancelled() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KeepAliveHandler.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
public void operationComplete(ChannelFuture future) throws Exception {
    if (future.isSuccess()) {
        logger.debug("{} PING write successful", channel);
        final EventLoop el = channel.eventLoop();
        shutdownFuture = el.schedule(shutdownRunnable, pingIdleTimeNanos, TimeUnit.NANOSECONDS);
        pingState = PingState.PENDING_PING_ACK;
        resetStopwatch();
    } else {
        // Mostly because the channel is already closed. So ignore and change state to IDLE.
        // If the channel is closed, we change state to SHUTDOWN on destroy.
        if (!future.isCancelled() && Exceptions.isExpected(future.cause())) {
            logger.debug("{} PING write failed", channel, future.cause());
        }
        if (pingState != PingState.SHUTDOWN) {
            pingState = PingState.IDLE;
        }
    }
}
 
Example 2
Source File: RedisExecutor.java    From redisson with Apache License 2.0 6 votes vote down vote up
private void checkWriteFuture(ChannelFuture future, RPromise<R> attemptPromise, RedisConnection connection) {
    if (future.isCancelled() || attemptPromise.isDone()) {
        return;
    }

    if (!future.isSuccess()) {
        exception = new WriteRedisConnectionException(
                "Unable to write command into connection! Node source: " + source + ", connection: " + connection +
                ", command: " + LogHelper.toString(command, params)
                + " after " + attempt + " retry attempts", future.cause());
        if (attempt == attempts) {
            attemptPromise.tryFailure(exception);
        }
        return;
    }

    timeout.cancel();

    scheduleResponseTimeout(attemptPromise, connection);
}
 
Example 3
Source File: BmpDispatcherImpl.java    From bgpcep with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void operationComplete(final ChannelFuture future) throws Exception {
    if (future.isCancelled()) {
        LOG.debug("Connection {} cancelled!", future);
    } else if (future.isSuccess()) {
        LOG.debug("Connection {} succeeded!", future);
        future.channel().closeFuture().addListener((ChannelFutureListener) channelFuture -> scheduleConnect());
    } else {
        if (this.delay > MAXIMUM_BACKOFF) {
            LOG.warn("The time of maximum backoff has been exceeded. No further connection attempts with BMP "
                    + "router {}.", this.remoteAddress);
            future.cancel(false);
            return;
        }
        final EventLoop loop = future.channel().eventLoop();
        loop.schedule(() -> this.bootstrap.connect().addListener(this), this.delay, TimeUnit.MILLISECONDS);
        LOG.info("The connection try to BMP router {} failed. Next reconnection attempt in {} milliseconds.",
                this.remoteAddress, this.delay);
        this.delay *= 2;
    }
}
 
Example 4
Source File: NettyClient.java    From octo-rpc with Apache License 2.0 5 votes vote down vote up
@Override
protected void doConnect() {
    ChannelFuture future = null;
    if (isClosed()) {
        logger.warn("{} closed, won't do channel connect", this.toString());
    }
    try {
        future = bootstrap.connect(getRemoteAddress());
        future.awaitUninterruptibly();
        if (future.isCancelled()) {
            throw new TransportException("Failed connect to server " + getRemoteAddress() + " from " + getClass().getName() + ", cause it be cancelled");
        } else if (!future.isSuccess()) {
            String errorMsg = future.cause() != null ? future.cause().getMessage() : "";
            throw new TransportException("Failed connect to server " + getRemoteAddress() + " from " + getClass().getName() + "[timeout:" + connTimeout + "ms], cause:" + errorMsg, future.cause());
        }
        NettyChannel oldChannel = this.channel;
        NettyChannel newChannel = ChannelManager.getOrAddChannel(future.channel());
        this.channel = newChannel;

        // 关闭旧的连接
        if (oldChannel != null) {
            if (oldChannel.isConnected()) {
                logger.info("Close old netty channel:{} and create new netty channel:{}", oldChannel, newChannel);
                oldChannel.close();
            }
        }

    } catch (Exception e) {
        if (e instanceof TransportException) {
            throw e;
        }
        throw new TransportException("Failed to connect to server " + getRemoteAddress(), e);
    } finally {
        if (future != null && !isConnected()) {
            future.cancel(true);
        }
    }
}
 
Example 5
Source File: RpcClient.java    From TakinRPC with Apache License 2.0 5 votes vote down vote up
@Override
public void operationComplete(ChannelFuture future) throws Exception {
    if (future.isDone() && future.isSuccess()) {
        set(new NettyRpcChannel(future.channel()));
    } else if (future.isDone() && future.cause() != null) {
        setException(future.cause());
    } else if (future.isDone() && future.isCancelled()) {
        cancel(false);
    }
}
 
Example 6
Source File: BmpMockDispatcher.java    From bgpcep with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void operationComplete(final ChannelFuture future) throws Exception {
    if (future.isCancelled()) {
        LOG.debug("Connection {} cancelled!", future);
    } else if (future.isSuccess()) {
        LOG.debug("Connection {} succeeded!", future);
        future.channel().closeFuture().addListener((ChannelFutureListener) channelFuture -> scheduleConnect());
    } else {
        final EventLoop loop = future.channel().eventLoop();
        loop.schedule(() -> this.bootstrap.connect().addListener(this), this.delay, TimeUnit.MILLISECONDS);
        LOG.info("The connection try to BMP router {} failed. Next reconnection attempt in {} milliseconds.",
                this.remoteAddress, this.delay);
    }
}
 
Example 7
Source File: HttpStreamEncoder.java    From netty-http2 with Apache License 2.0 5 votes vote down vote up
public void operationComplete(ChannelFuture future) throws Exception {
    if (future.isSuccess()) {
        ctx.writeAndFlush(msg, promise);
    } else if (future.isCancelled()) {
        ReferenceCountUtil.release(msg);
        promise.cancel(true);
    } else {
        ReferenceCountUtil.release(msg);
        promise.setFailure(future.cause());
    }
}