我需要连接到服务器,我知道它将在端口上侦听。虽然可能需要一些时间才能投入使用。是否可以让ClientBootstrap尝试连接给定次数的尝试或直到达到超时?
目前,如果连接被拒绝,我会收到异常,但它应该尝试在后台连接,例如尊重“connectTimeoutMillis”引导程序选项。
答案 0 :(得分:4)
你需要手工完成,但这并不难......
你可以这样做:
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);
}
}
}
});