Netty: ClientBootstrap connection attempts

I need to connect to a server that I know will be listening on a port. It may take a while to get started though. Is it possible to force ClientBootstrap to try to connect for a specific number of attempts, or until a timeout is reached?

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


source to share

1 answer

You need to do it manually, but it's not difficult.

You can 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




All Articles