Netty: ClientBootstrap connect retries

旧街凉风 提交于 2021-02-19 03:40:08

问题


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?

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.


回答1:


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

You could do something like this:

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);
            }
        }
    }
});


来源:https://stackoverflow.com/questions/9873237/netty-clientbootstrap-connect-retries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!