Example usage for io.netty.channel ChannelFuture isSuccess

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

Introduction

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

Prototype

boolean isSuccess();

Source Link

Document

Returns true if and only if the I/O operation was completed successfully.

Usage

From source file:com.alibaba.rocketmq.remoting.netty.NettyRemotingAbstract.java

License:Apache License

public void invokeAsyncImpl(final Channel channel, final RemotingCommand request, final long timeoutMillis,
        final InvokeCallback invokeCallback) throws InterruptedException, RemotingTooMuchRequestException,
        RemotingTimeoutException, RemotingSendRequestException {
    boolean acquired = this.semaphoreAsync.tryAcquire(timeoutMillis, TimeUnit.MILLISECONDS);
    if (acquired) {
        final SemaphoreReleaseOnlyOnce once = new SemaphoreReleaseOnlyOnce(this.semaphoreAsync);

        final ResponseFuture responseFuture = new ResponseFuture(request.getOpaque(), timeoutMillis,
                invokeCallback, once);/*from   w  w w . java2  s  .  c  o m*/
        this.responseTable.put(request.getOpaque(), responseFuture);
        try {
            channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture f) throws Exception {
                    if (f.isSuccess()) {
                        responseFuture.setSendRequestOK(true);
                        return;
                    } else {
                        responseFuture.setSendRequestOK(false);
                    }

                    responseFuture.putResponse(null);
                    responseTable.remove(request.getOpaque());
                    try {
                        responseFuture.executeInvokeCallback();
                    } catch (Throwable e) {
                        plog.warn("excute callback in writeAndFlush addListener, and callback throw", e);
                    } finally {
                        responseFuture.release();
                    }

                    plog.warn("send a request command to channel <{}> failed.",
                            RemotingHelper.parseChannelRemoteAddr(channel));
                    plog.warn(request.toString());
                }
            });
        } catch (Exception e) {
            responseFuture.release();
            plog.warn("send a request command to channel <" + RemotingHelper.parseChannelRemoteAddr(channel)
                    + "> Exception", e);
            throw new RemotingSendRequestException(RemotingHelper.parseChannelRemoteAddr(channel), e);
        }
    } else {
        if (timeoutMillis <= 0) {
            throw new RemotingTooMuchRequestException("invokeAsyncImpl invoke too fast");
        } else {
            String info = String.format(
                    "invokeAsyncImpl tryAcquire semaphore timeout, %dms, waiting thread nums: %d semaphoreAsyncValue: %d", //
                    timeoutMillis, //
                    this.semaphoreAsync.getQueueLength(), //
                    this.semaphoreAsync.availablePermits()//
            );
            plog.warn(info);
            plog.warn(request.toString());
            throw new RemotingTimeoutException(info);
        }
    }
}

From source file:com.alibaba.rocketmq.remoting.netty.NettyRemotingAbstract.java

License:Apache License

public void invokeOnewayImpl(final Channel channel, final RemotingCommand request, final long timeoutMillis)
        throws InterruptedException, RemotingTooMuchRequestException, RemotingTimeoutException,
        RemotingSendRequestException {/*from   ww w  .  j a  va 2 s . c  o  m*/
    request.markOnewayRPC();
    boolean acquired = this.semaphoreOneway.tryAcquire(timeoutMillis, TimeUnit.MILLISECONDS);
    if (acquired) {
        final SemaphoreReleaseOnlyOnce once = new SemaphoreReleaseOnlyOnce(this.semaphoreOneway);
        try {
            channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture f) throws Exception {
                    once.release();
                    if (!f.isSuccess()) {
                        plog.warn(
                                "send a request command to channel <" + channel.remoteAddress() + "> failed.");
                        plog.warn(request.toString());
                    }
                }
            });
        } catch (Exception e) {
            once.release();
            plog.warn("write send a request command to channel <" + channel.remoteAddress() + "> failed.");
            throw new RemotingSendRequestException(RemotingHelper.parseChannelRemoteAddr(channel), e);
        }
    } else {
        if (timeoutMillis <= 0) {
            throw new RemotingTooMuchRequestException("invokeOnewayImpl invoke too fast");
        } else {
            String info = String.format(
                    "invokeOnewayImpl tryAcquire semaphore timeout, %dms, waiting thread nums: %d semaphoreAsyncValue: %d", //
                    timeoutMillis, //
                    this.semaphoreAsync.getQueueLength(), //
                    this.semaphoreAsync.availablePermits()//
            );
            plog.warn(info);
            plog.warn(request.toString());
            throw new RemotingTimeoutException(info);
        }
    }
}

From source file:com.amebame.triton.service.lock.LockOwner.java

License:Creative Commons License

/**
 * Send ready to the owner client.//from ww  w  .  ja v a 2  s .  c  o  m
 * @return
 */
public boolean sendReady() {
    TritonMessage message = new TritonMessage(TritonMessage.REPLY, callId, ownerId);
    if (!channel.isOpen()) {
        return false;
    }
    ChannelFuture future = channel.writeAndFlush(message);
    try {
        if (future.await(1000L)) {
            return future.isSuccess();
        }
    } catch (InterruptedException e) {
    }
    return false;
}

From source file:com.amebame.triton.service.lock.LockOwner.java

License:Creative Commons License

/**
 * Send fail to the owner client.// w w w .j a  v  a2  s  .  co m
 * @return
 */
public boolean sendFail() {
    if (!channel.isWritable()) {
        return false;
    }
    TritonMessage message = new TritonMessage(TritonMessage.REPLY, callId, -1);
    ChannelFuture future = channel.writeAndFlush(message);
    try {
        if (future.await(1000L)) {
            return future.isSuccess();
        }
    } catch (InterruptedException e) {
    }
    return false;
}

From source file:com.andrewkroh.cisco.xmlservices.ChannelConnectListener.java

License:Apache License

@Override
public void operationComplete(ChannelFuture future) throws Exception {
    LOGGER.debug("connect() complete, status: " + future);

    if (responseFuture.isCancelled()) {
        future.channel().close();/*from   w  ww  .  jav  a  2s. c  o  m*/
        return;
    }

    if (future.isSuccess()) {
        final Channel channel = future.channel();
        channel.attr(phoneAttributeKey).set(phone);
        channel.attr(responseFutureAttributeKey).set(responseFuture);

        // Timeout the task if it does not complete:
        eventLoopExecutor.schedule(new Runnable() {
            @Override
            public void run() {
                if (!responseFuture.isDone()) {
                    responseFuture.cancel(false);
                    channel.close();
                }
            }
        }, responseTimeoutMs, TimeUnit.MILLISECONDS);

        channel.writeAndFlush(httpRequest).addListener(new ChannelWriteFuture<T>(responseFuture));
    } else {
        responseFuture.setException(future.cause());
        future.channel().close();
    }
}

From source file:com.andrewkroh.cisco.xmlservices.ChannelWriteFuture.java

License:Apache License

@Override
public void operationComplete(ChannelFuture future) throws Exception {
    LOGGER.debug("write() complete, status: " + future);

    if (!future.isSuccess()) {
        responseFuture.setException(future.cause());
        future.channel().close();//ww w .ja  v a2s.c  om
    }
}

From source file:com.andy.grpc.proxy.HexDumpProxyBackendHandler.java

License:Apache License

@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) {
    inboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
        @Override//from  w ww  . j  a  v  a  2s.  co  m
        public void operationComplete(ChannelFuture future) {
            if (future.isSuccess()) {
                ctx.channel().read();
            } else {
                future.channel().close();
            }
        }
    });
}

From source file:com.athena.meerkat.agent.netty.MeerkatClientListener.java

License:Apache License

public void operationComplete(ChannelFuture future) throws Exception {
    if (future.isSuccess()) {
        future.sync();/*from ww  w  .jav a 2 s.c  o m*/
    } else {
        // ? ?  5  ?? .
        final EventLoop loop = future.channel().eventLoop();

        loop.schedule(new Runnable() {
            public void run() {
                LOGGER.debug("Attempt to reconnect within 5 seconds.");
                client.createBootstrap(new Bootstrap(), loop, host);
            }
        }, 5L, TimeUnit.SECONDS);
    }
}

From source file:com.athena.peacock.agent.netty.PeacockClientListener.java

License:Apache License

@Override
public void operationComplete(ChannelFuture future) throws Exception {
    if (future.isSuccess()) {
        future.sync();/*from   w w  w .  ja  v a2s.  c  o m*/
    } else {
        // ? ?  5  ?? .
        final EventLoop loop = future.channel().eventLoop();

        loop.schedule(new Runnable() {
            @Override
            public void run() {
                logger.debug("Attempt to reconnect within 5 seconds.");
                client.createBootstrap(new Bootstrap(), loop, host);
            }
        }, 5L, TimeUnit.SECONDS);
    }
}

From source file:com.avanza.astrix.netty.client.NettyRemotingClient.java

License:Apache License

public void connect(String host, int port) {
    Bootstrap b = new Bootstrap();
    b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
        @Override/*from   w  w  w  .ja va  2 s.co m*/
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
                    nettyRemotingClientHandler);
        }
    });

    // Start the connection attempt.
    ChannelFuture channel = b.connect(host, port);
    try {
        if (channel.await(1, TimeUnit.SECONDS)) {
            if (channel.isSuccess()) {
                return;
            }
        }
    } catch (InterruptedException e) {
    }
    destroy();
    throw new IllegalArgumentException(
            String.format("Failed to connect to remoting server: %s:%d", host, port));
}