Java Code Examples for org.jboss.netty.channel.ChannelFuture#cancel()

The following examples show how to use org.jboss.netty.channel.ChannelFuture#cancel() . 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: NettyRpcConnection.java    From voyage with Apache License 2.0 5 votes vote down vote up
/**
 * 尝试连接
 */
public void connect() {
       ChannelFuture future = bootstrap.connect(inetAddr);
       try{
           boolean ret = future.awaitUninterruptibly(Constants.TIMEOUT_CONNECTION_MILLSECOND, TimeUnit.MILLISECONDS);
           if (ret && future.isSuccess()) {
               Channel newChannel = future.getChannel();
               newChannel.setInterestOps(Channel.OP_READ_WRITE);
               try {
                   // 关闭旧的连接
                   Channel oldChannel = NettyRpcConnection.this.channel;
                   if (oldChannel != null) {
                       logger.info("Close old netty channel {} on create new netty channel {}", oldChannel, newChannel);
                       oldChannel.close();
                   }
               } finally {
                   if (!isConnected()) {
                       try {
                           logger.info("Close new netty channel {}, because the client closed.", newChannel);
                           newChannel.close();
                       } finally {
                       	NettyRpcConnection.this.channel = null;
                       }
                   } else {
                   	NettyRpcConnection.this.channel = newChannel;
                   }
               }
           } else if (null != future.getCause()) {
           	logger.error("connect fail", future.getCause());
           	throw new RuntimeException("connect error", future.getCause());
           } else {
           	logger.error("connect fail,connstr: "+this.getConnStr());
           	throw new RuntimeException("connect error");
           }
       }finally{
           if (! isConnected()) {
               future.cancel();
           }
       }
}
 
Example 2
Source File: NettyClient.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
@Override
protected void doConnect() throws Throwable {
    long start = System.currentTimeMillis();
    ChannelFuture future = bootstrap.connect(getConnectAddress());
    try {
        boolean ret = future.awaitUninterruptibly(getConnectTimeout(), TimeUnit.MILLISECONDS);

        if (ret && future.isSuccess()) {
            Channel newChannel = future.getChannel();
            newChannel.setInterestOps(Channel.OP_READ_WRITE);
            try {
                // Close old channel
                Channel oldChannel = NettyClient.this.channel; // copy reference
                if (oldChannel != null) {
                    try {
                        if (logger.isInfoEnabled()) {
                            logger.info("Close old netty channel " + oldChannel + " on create new netty channel " + newChannel);
                        }
                        oldChannel.close();
                    } finally {
                        NettyChannel.removeChannelIfDisconnected(oldChannel);
                    }
                }
            } finally {
                if (NettyClient.this.isClosed()) {
                    try {
                        if (logger.isInfoEnabled()) {
                            logger.info("Close new netty channel " + newChannel + ", because the client closed.");
                        }
                        newChannel.close();
                    } finally {
                        NettyClient.this.channel = null;
                        NettyChannel.removeChannelIfDisconnected(newChannel);
                    }
                } else {
                    NettyClient.this.channel = newChannel;
                }
            }
        } else if (future.getCause() != null) {
            throw new RemotingException(this, "client(url: " + getUrl() + ") failed to connect to server "
                    + getRemoteAddress() + ", error message is:" + future.getCause().getMessage(), future.getCause());
        } else {
            throw new RemotingException(this, "client(url: " + getUrl() + ") failed to connect to server "
                    + getRemoteAddress() + " client-side timeout "
                    + getConnectTimeout() + "ms (elapsed: " + (System.currentTimeMillis() - start) + "ms) from netty client "
                    + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion());
        }
    } finally {
        if (!isConnected()) {
            future.cancel();
        }
    }
}
 
Example 3
Source File: NettyTransport.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
protected void connectToChannels(NodeChannels nodeChannels, DiscoveryNode node) {
    ChannelFuture[] connectRecovery = new ChannelFuture[nodeChannels.recovery.length];
    ChannelFuture[] connectBulk = new ChannelFuture[nodeChannels.bulk.length];
    ChannelFuture[] connectReg = new ChannelFuture[nodeChannels.reg.length];
    ChannelFuture[] connectState = new ChannelFuture[nodeChannels.state.length];
    ChannelFuture[] connectPing = new ChannelFuture[nodeChannels.ping.length];
    InetSocketAddress address = ((InetSocketTransportAddress) node.address()).address();
    for (int i = 0; i < connectRecovery.length; i++) {
        connectRecovery[i] = clientBootstrap.connect(address);
    }
    for (int i = 0; i < connectBulk.length; i++) {
        connectBulk[i] = clientBootstrap.connect(address);
    }
    for (int i = 0; i < connectReg.length; i++) {
        connectReg[i] = clientBootstrap.connect(address);
    }
    for (int i = 0; i < connectState.length; i++) {
        connectState[i] = clientBootstrap.connect(address);
    }
    for (int i = 0; i < connectPing.length; i++) {
        connectPing[i] = clientBootstrap.connect(address);
    }

    try {
        for (int i = 0; i < connectRecovery.length; i++) {
            connectRecovery[i].awaitUninterruptibly((long) (connectTimeout.millis() * 1.5));
            if (!connectRecovery[i].isSuccess()) {
                throw new ConnectTransportException(node, "connect_timeout[" + connectTimeout + "]", connectRecovery[i].getCause());
            }
            nodeChannels.recovery[i] = connectRecovery[i].getChannel();
            nodeChannels.recovery[i].getCloseFuture().addListener(new ChannelCloseListener(node));
        }

        for (int i = 0; i < connectBulk.length; i++) {
            connectBulk[i].awaitUninterruptibly((long) (connectTimeout.millis() * 1.5));
            if (!connectBulk[i].isSuccess()) {
                throw new ConnectTransportException(node, "connect_timeout[" + connectTimeout + "]", connectBulk[i].getCause());
            }
            nodeChannels.bulk[i] = connectBulk[i].getChannel();
            nodeChannels.bulk[i].getCloseFuture().addListener(new ChannelCloseListener(node));
        }

        for (int i = 0; i < connectReg.length; i++) {
            connectReg[i].awaitUninterruptibly((long) (connectTimeout.millis() * 1.5));
            if (!connectReg[i].isSuccess()) {
                throw new ConnectTransportException(node, "connect_timeout[" + connectTimeout + "]", connectReg[i].getCause());
            }
            nodeChannels.reg[i] = connectReg[i].getChannel();
            nodeChannels.reg[i].getCloseFuture().addListener(new ChannelCloseListener(node));
        }

        for (int i = 0; i < connectState.length; i++) {
            connectState[i].awaitUninterruptibly((long) (connectTimeout.millis() * 1.5));
            if (!connectState[i].isSuccess()) {
                throw new ConnectTransportException(node, "connect_timeout[" + connectTimeout + "]", connectState[i].getCause());
            }
            nodeChannels.state[i] = connectState[i].getChannel();
            nodeChannels.state[i].getCloseFuture().addListener(new ChannelCloseListener(node));
        }

        for (int i = 0; i < connectPing.length; i++) {
            connectPing[i].awaitUninterruptibly((long) (connectTimeout.millis() * 1.5));
            if (!connectPing[i].isSuccess()) {
                throw new ConnectTransportException(node, "connect_timeout[" + connectTimeout + "]", connectPing[i].getCause());
            }
            nodeChannels.ping[i] = connectPing[i].getChannel();
            nodeChannels.ping[i].getCloseFuture().addListener(new ChannelCloseListener(node));
        }

        if (nodeChannels.recovery.length == 0) {
            if (nodeChannels.bulk.length > 0) {
                nodeChannels.recovery = nodeChannels.bulk;
            } else {
                nodeChannels.recovery = nodeChannels.reg;
            }
        }
        if (nodeChannels.bulk.length == 0) {
            nodeChannels.bulk = nodeChannels.reg;
        }
    } catch (RuntimeException e) {
        // clean the futures
        List<ChannelFuture> futures = new ArrayList<>();
        futures.addAll(Arrays.asList(connectRecovery));
        futures.addAll(Arrays.asList(connectBulk));
        futures.addAll(Arrays.asList(connectReg));
        futures.addAll(Arrays.asList(connectState));
        futures.addAll(Arrays.asList(connectPing));
        for (ChannelFuture future : Collections.unmodifiableList(futures)) {
            future.cancel();
            if (future.getChannel() != null && future.getChannel().isOpen()) {
                try {
                    future.getChannel().close();
                } catch (Exception e1) {
                    // ignore
                }
            }
        }
        throw e;
    }
}
 
Example 4
Source File: NettyClient.java    From dubbox with Apache License 2.0 4 votes vote down vote up
protected void doConnect() throws Throwable {
    long start = System.currentTimeMillis();
    ChannelFuture future = bootstrap.connect(getConnectAddress());
    try{
        boolean ret = future.awaitUninterruptibly(getConnectTimeout(), TimeUnit.MILLISECONDS);
        
        if (ret && future.isSuccess()) {
            Channel newChannel = future.getChannel();
            newChannel.setInterestOps(Channel.OP_READ_WRITE);
            try {
                // 关闭旧的连接
                Channel oldChannel = NettyClient.this.channel; // copy reference
                if (oldChannel != null) {
                    try {
                        if (logger.isInfoEnabled()) {
                            logger.info("Close old netty channel " + oldChannel + " on create new netty channel " + newChannel);
                        }
                        oldChannel.close();
                    } finally {
                        NettyChannel.removeChannelIfDisconnected(oldChannel);
                    }
                }
            } finally {
                if (NettyClient.this.isClosed()) {
                    try {
                        if (logger.isInfoEnabled()) {
                            logger.info("Close new netty channel " + newChannel + ", because the client closed.");
                        }
                        newChannel.close();
                    } finally {
                        NettyClient.this.channel = null;
                        NettyChannel.removeChannelIfDisconnected(newChannel);
                    }
                } else {
                    NettyClient.this.channel = newChannel;
                }
            }
        } else if (future.getCause() != null) {
            throw new RemotingException(this, "client(url: " + getUrl() + ") failed to connect to server "
                    + getRemoteAddress() + ", error message is:" + future.getCause().getMessage(), future.getCause());
        } else {
            throw new RemotingException(this, "client(url: " + getUrl() + ") failed to connect to server "
                    + getRemoteAddress() + " client-side timeout "
                    + getConnectTimeout() + "ms (elapsed: " + (System.currentTimeMillis() - start) + "ms) from netty client "
                    + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion());
        }
    }finally{
        if (! isConnected()) {
            future.cancel();
        }
    }
}
 
Example 5
Source File: NettyClient.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
protected void doConnect() throws Throwable {
    long start = System.currentTimeMillis();
    ChannelFuture future = bootstrap.connect(getConnectAddress());
    try{
        boolean ret = future.awaitUninterruptibly(getConnectTimeout(), TimeUnit.MILLISECONDS);
        
        if (ret && future.isSuccess()) {
            Channel newChannel = future.getChannel();
            newChannel.setInterestOps(Channel.OP_READ_WRITE);
            try {
                // 关闭旧的连接
                Channel oldChannel = NettyClient.this.channel; // copy reference
                if (oldChannel != null) {
                    try {
                        if (logger.isInfoEnabled()) {
                            logger.info("Close old netty channel " + oldChannel + " on create new netty channel " + newChannel);
                        }
                        oldChannel.close();
                    } finally {
                        NettyChannel.removeChannelIfDisconnected(oldChannel);
                    }
                }
            } finally {
                if (NettyClient.this.isClosed()) {
                    try {
                        if (logger.isInfoEnabled()) {
                            logger.info("Close new netty channel " + newChannel + ", because the client closed.");
                        }
                        newChannel.close();
                    } finally {
                        NettyClient.this.channel = null;
                        NettyChannel.removeChannelIfDisconnected(newChannel);
                    }
                } else {
                    NettyClient.this.channel = newChannel;
                }
            }
        } else if (future.getCause() != null) {
            throw new RemotingException(this, "client(url: " + getUrl() + ") failed to connect to server "
                    + getRemoteAddress() + ", error message is:" + future.getCause().getMessage(), future.getCause());
        } else {
            throw new RemotingException(this, "client(url: " + getUrl() + ") failed to connect to server "
                    + getRemoteAddress() + " client-side timeout "
                    + getConnectTimeout() + "ms (elapsed: " + (System.currentTimeMillis() - start) + "ms) from netty client "
                    + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion());
        }
    }finally{
        if (! isConnected()) {
            future.cancel();
        }
    }
}
 
Example 6
Source File: NettyClient.java    From dubbox with Apache License 2.0 4 votes vote down vote up
protected void doConnect() throws Throwable {
    long start = System.currentTimeMillis();
    ChannelFuture future = bootstrap.connect(getConnectAddress());
    try{
        boolean ret = future.awaitUninterruptibly(getConnectTimeout(), TimeUnit.MILLISECONDS);
        
        if (ret && future.isSuccess()) {
            Channel newChannel = future.getChannel();
            newChannel.setInterestOps(Channel.OP_READ_WRITE);
            try {
                // 关闭旧的连接
                Channel oldChannel = NettyClient.this.channel; // copy reference
                if (oldChannel != null) {
                    try {
                        if (logger.isInfoEnabled()) {
                            logger.info("Close old netty channel " + oldChannel + " on create new netty channel " + newChannel);
                        }
                        oldChannel.close();
                    } finally {
                        NettyChannel.removeChannelIfDisconnected(oldChannel);
                    }
                }
            } finally {
                if (NettyClient.this.isClosed()) {
                    try {
                        if (logger.isInfoEnabled()) {
                            logger.info("Close new netty channel " + newChannel + ", because the client closed.");
                        }
                        newChannel.close();
                    } finally {
                        NettyClient.this.channel = null;
                        NettyChannel.removeChannelIfDisconnected(newChannel);
                    }
                } else {
                    NettyClient.this.channel = newChannel;
                }
            }
        } else if (future.getCause() != null) {
            throw new RemotingException(this, "client(url: " + getUrl() + ") failed to connect to server "
                    + getRemoteAddress() + ", error message is:" + future.getCause().getMessage(), future.getCause());
        } else {
            throw new RemotingException(this, "client(url: " + getUrl() + ") failed to connect to server "
                    + getRemoteAddress() + " client-side timeout "
                    + getConnectTimeout() + "ms (elapsed: " + (System.currentTimeMillis() - start) + "ms) from netty client "
                    + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion());
        }
    }finally{
        if (! isConnected()) {
            future.cancel();
        }
    }
}
 
Example 7
Source File: NettyClient.java    From anima with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void doConnect() throws Throwable {
	long start = System.currentTimeMillis();
       ChannelFuture future = bootstrap.connect(getConnectAddress());
       try{
           boolean ret = future.awaitUninterruptibly(getConnectTimeout(), TimeUnit.MILLISECONDS);
           
           if (ret && future.isSuccess()) {
               Channel newChannel = future.getChannel();
               newChannel.setInterestOps(Channel.OP_READ_WRITE);
               try {

                   Channel oldChannel = NettyClient.this.channel; 
                   if (oldChannel != null) {
                       try {
                           if (logger.isInfoEnabled()) {
                               logger.info("Close old netty channel " + oldChannel + " on create new netty channel " + newChannel);
                           }
                           oldChannel.close();
                       } finally {
                           NettyChannel.removeChannelIfDisconnected(oldChannel);
                       }
                   }
               } finally {
                   if (NettyClient.this.isClosed()) {
                       try {
                           if (logger.isInfoEnabled()) {
                               logger.info("Close new netty channel " + newChannel + ", because the client closed.");
                           }
                           newChannel.close();
                       } finally {
                           NettyClient.this.channel = null;
                           NettyChannel.removeChannelIfDisconnected(newChannel);
                       }
                   } else {
                       NettyClient.this.channel = newChannel;
                   }
               }
           } else if (future.getCause() != null) {
               throw new RemotingException(this, "client failed to connect to server "
                       + getRemoteAddress() + ", error message is:" + future.getCause().getMessage(), future.getCause());
           } else {
               throw new RemotingException(this, "client failed to connect to server "
                       + getRemoteAddress() + " client-side timeout "
                       + getConnectTimeout() + "ms (elapsed: " + (System.currentTimeMillis() - start) + "ms) from netty client");
           }
       }finally{
           if (! isConnected()) {
               future.cancel();
           }
       }
}
 
Example 8
Source File: NettyClient.java    From dubbox with Apache License 2.0 4 votes vote down vote up
protected void doConnect() throws Throwable {
    long start = System.currentTimeMillis();
    ChannelFuture future = bootstrap.connect(getConnectAddress());
    try{
        boolean ret = future.awaitUninterruptibly(getConnectTimeout(), TimeUnit.MILLISECONDS);
        
        if (ret && future.isSuccess()) {
            Channel newChannel = future.getChannel();
            newChannel.setInterestOps(Channel.OP_READ_WRITE);
            try {
                // 关闭旧的连接
                Channel oldChannel = NettyClient.this.channel; // copy reference
                if (oldChannel != null) {
                    try {
                        if (logger.isInfoEnabled()) {
                            logger.info("Close old netty channel " + oldChannel + " on create new netty channel " + newChannel);
                        }
                        oldChannel.close();
                    } finally {
                        NettyChannel.removeChannelIfDisconnected(oldChannel);
                    }
                }
            } finally {
                if (NettyClient.this.isClosed()) {
                    try {
                        if (logger.isInfoEnabled()) {
                            logger.info("Close new netty channel " + newChannel + ", because the client closed.");
                        }
                        newChannel.close();
                    } finally {
                        NettyClient.this.channel = null;
                        NettyChannel.removeChannelIfDisconnected(newChannel);
                    }
                } else {
                    NettyClient.this.channel = newChannel;
                }
            }
        } else if (future.getCause() != null) {
            throw new RemotingException(this, "client(url: " + getUrl() + ") failed to connect to server "
                    + getRemoteAddress() + ", error message is:" + future.getCause().getMessage(), future.getCause());
        } else {
            throw new RemotingException(this, "client(url: " + getUrl() + ") failed to connect to server "
                    + getRemoteAddress() + " client-side timeout "
                    + getConnectTimeout() + "ms (elapsed: " + (System.currentTimeMillis() - start) + "ms) from netty client "
                    + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion());
        }
    }finally{
        if (! isConnected()) {
            future.cancel();
        }
    }
}