Netty:ClientBootstrap 连接重试 [英] Netty: ClientBootstrap connect retries

查看:65
本文介绍了Netty:ClientBootstrap 连接重试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要连接到一个服务器,我知道它会监听一个端口.尽管可能需要一些时间才能投入使用.是否可以让 ClientBootstrap 尝试连接给定的尝试次数或直到达到超时?

I need to connect to a server, which I know will be listening on a port. Although It could take some time to get operational. Is it possible to get ClientBootstrap to try to connect for a given number of tries or until a timeout is reached?

目前,如果连接被拒绝,我会收到一个异常,但它应该尝试在后台连接,例如通过尊重connectTimeoutMillis"引导选项.

At the moment, if the connection is refused, I get an exception, but it should try to connect in background, for example by respecting the "connectTimeoutMillis" bootstrap option.

推荐答案

你需要手动完成,但这并不难..

You need todo it by hand, but thats not hard..

你可以这样做:

final ClientBootstrap bs = new ClientBootstrap(...);
final InetSocketAddress address = new InetSocketAddress("remoteip", 110);
final int maxretries = 5;
final AtomicInteger count = new AtomicInteger();
bs.connect(address).addListener(new ChannelFutureListener() {

    public void operationComplete(ChannelFuture future) throws Exception {
        if (!future.isSuccess()) {
            if (count.incrementAndGet() > maxretries) {
                // fails to connect even after maxretries do something
            } else {
                // retry
                bs.connect(address).addListener(this);
            }
        }
    }
});

这篇关于Netty:ClientBootstrap 连接重试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆