Example usage for io.netty.channel ChannelFuture cause

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

Introduction

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

Prototype

Throwable cause();

Source Link

Document

Returns the cause of the failed I/O operation if the I/O operation has failed.

Usage

From source file:com.diwayou.hybrid.remoting.netty.NettyRemotingAbstract.java

License:Apache License

public RemotingCommand invokeSyncImpl(final Channel channel, final RemotingCommand request,
        final long timeoutMillis)
        throws InterruptedException, RemotingSendRequestException, RemotingTimeoutException {
    try {/*from  w w  w . j  a va  2  s.  c o  m*/
        final ResponseFuture responseFuture = new ResponseFuture(request.getOpaque(), timeoutMillis, null,
                null);
        this.responseTable.put(request.getOpaque(), responseFuture);
        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);
                }

                responseTable.remove(request.getOpaque());
                responseFuture.setCause(f.cause());
                responseFuture.putResponse(null);
                plog.warn("send a request command to channel <" + channel.remoteAddress() + "> failed.");
                plog.warn(request.toString());
            }
        });

        RemotingCommand responseCommand = responseFuture.waitResponse(timeoutMillis);
        if (null == responseCommand) {
            // ????
            if (responseFuture.isSendRequestOK()) {
                throw new RemotingTimeoutException(RemotingHelper.parseChannelRemoteAddr(channel),
                        timeoutMillis, responseFuture.getCause());
            }
            // ??
            else {
                throw new RemotingSendRequestException(RemotingHelper.parseChannelRemoteAddr(channel),
                        responseFuture.getCause());
            }
        }

        return responseCommand;
    } finally {
        this.responseTable.remove(request.getOpaque());
    }
}

From source file:com.ebay.jetstream.messaging.transport.netty.autoflush.handler.AutoFlushWriterChannelListener.java

License:MIT License

@Override
public void operationComplete(ChannelFuture future) throws Exception {

    if (future.isSuccess()) {
        ExtendedChannelPromise bPromise = (ExtendedChannelPromise) future;
        ExtendedChannelPromise firstEvent = (ExtendedChannelPromise) (m_events[0].getPromise());
        firstEvent.setWrittenSize(bPromise.getWrittenSize());
        firstEvent.setRawBytes(bPromise.getRawBytes());
        firstEvent.setCompressedBytes(bPromise.getCompressedBytes());
        for (int i = 0; i < m_events.length; i++) {
            m_events[i].getPromise().setSuccess(null);

        }/*  ww  w.ja  va2 s .  c  om*/
    } else {
        Throwable cause = future.cause();
        for (int i = 0; i < m_events.length; i++) {
            m_events[i].getPromise().setFailure(cause);
        }
    }

}

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);/*  ww  w . j  a v  a2 s.  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.android.CIMConnectorManager.java

License:Apache License

public void send(SentBody body) {

    boolean isSuccessed = false;

    String exceptionName = SessionClosedException.class.getSimpleName();

    if (isConnected()) {
        ChannelFuture future = channel.writeAndFlush(body);
        isSuccessed = future.awaitUninterruptibly(WRITE_TIMEOUT);
        if (!isSuccessed && future.cause() != null) {
            exceptionName = future.cause().getClass().getSimpleName();
        }//from www .  ja v  a2  s  .  com

    }

    if (!isSuccessed) {
        Intent intent = new Intent();
        intent.setAction(CIMConstant.IntentAction.ACTION_SENT_FAILED);
        intent.putExtra(Exception.class.getName(), exceptionName);
        intent.putExtra(SentBody.class.getName(), body);
        context.sendBroadcast(intent);
    } else {
        Intent intent = new Intent();
        intent.setAction(CIMConstant.IntentAction.ACTION_SENT_SUCCESSED);
        intent.putExtra(SentBody.class.getName(), (SentBody) body);
        context.sendBroadcast(intent);
    }

}

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  w w  w  .j a v  a2  s  .c om*/
    }

    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.flysoloing.learning.network.netty.redis.RedisClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {//from  w w  w.  ja v a 2  s.com
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                p.addLast(new RedisDecoder());
                p.addLast(new RedisBulkStringAggregator());
                p.addLast(new RedisArrayAggregator());
                p.addLast(new RedisEncoder());
                p.addLast(new RedisClientHandler());
            }
        });

        // Start the connection attempt.
        Channel ch = b.connect(HOST, PORT).sync().channel();

        // Read commands from the stdin.
        System.out.println("Enter Redis commands (quit to end)");
        ChannelFuture lastWriteFuture = null;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        for (;;) {
            final String input = in.readLine();
            final String line = input != null ? input.trim() : null;
            if (line == null || "quit".equalsIgnoreCase(line)) { // EOF or "quit"
                ch.close().sync();
                break;
            } else if (line.isEmpty()) { // skip `enter` or `enter` with spaces.
                continue;
            }
            // Sends the received line to the server.
            lastWriteFuture = ch.writeAndFlush(line);
            lastWriteFuture.addListener(new GenericFutureListener<ChannelFuture>() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (!future.isSuccess()) {
                        System.err.print("write failed: ");
                        future.cause().printStackTrace(System.err);
                    }
                }
            });
        }

        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null) {
            lastWriteFuture.sync();
        }
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.flysoloing.learning.network.netty.uptime.UptimeClient.java

License:Apache License

static void connect(Bootstrap b) {
    b.connect().addListener(new ChannelFutureListener() {
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.cause() != null) {
                handler.startTime = -1;//from ww  w  .ja  v  a2s . c om
                handler.println("Failed to connect: " + future.cause());
            }
        }
    });
}

From source file:com.github.lburgazzoli.quickfixj.transport.netty.NettySocketInitiator.java

License:Apache License

/**
 *
 *///  w w w.  ja  va 2 s  . c  om
private void doConnect() {
    ChannelFuture future = m_boot.connect();
    future.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) {
            if (future.isDone() && future.isSuccess()) {
                setChannel(new NettyChannel(future.channel()));
            } else if (!future.isSuccess() && !future.isCancelled()) {
                LOGGER.warn("Error", future.cause());
                doReconnect();
            }
        }
    });
}

From source file:com.github.mrstampy.gameboot.netty.NettyConnectionRegistry.java

License:Open Source License

private void log(ChannelFuture f, Object key) {
    if (f.isSuccess()) {
        log.debug("Successful send to {} on {}", key, f.channel());
    } else {//ww  w  . ja v  a2 s.c  om
        log.error("Failed sending to {} on {}", key, f.channel(), f.cause());
    }
}

From source file:com.github.mrstampy.kitchensync.netty.Bootstrapper.java

License:Open Source License

private GenericFutureListener<ChannelFuture> getBindListener(final int port, final CountDownLatch latch) {
    return new GenericFutureListener<ChannelFuture>() {

        @Override// w  ww. java2  s  . c om
        public void operationComplete(ChannelFuture future) throws Exception {
            try {
                if (future.isSuccess()) {
                    log.debug("Channel creation successful for {}", future.channel());
                } else {
                    Throwable cause = future.cause();
                    if (cause == null) {
                        log.error("Could not create channel for {}", port);
                    } else {
                        log.error("Could not create channel for {}", port, cause);
                    }
                }
            } finally {
                latch.countDown();
            }
        }
    };
}