Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netty: ClientBootstrap connect retries

Tags:

java

netty

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.

like image 939
Paulo Fidalgo Avatar asked Dec 31 '25 22:12

Paulo Fidalgo


1 Answers

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);
            }
        }
    }
});
like image 153
Norman Maurer Avatar answered Jan 02 '26 12:01

Norman Maurer