问题
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