检查 Netty 服务器是否启动时有其他睡眠方法吗?

Alternatives to sleep when checking if Netty server is up?

我正在启动带有休息界面的 Netty。我得到这个例外:

javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request
    at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:287)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:436)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:102)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:64)
    at com.sun.proxy.$Proxy20.ping(Unknown Source)
    at com.openet.atf.agent.proxy.SlaveRemoteProxy.ping(SlaveRemoteProxy.java:41)
    at com.openet.atf.agent.manage.Master.startSlaves(Master.java:86)
    at com.openet.acceptance.runner.AcceptanceRunner.main(AcceptanceRunner.java:195)
Caused by: org.apache.http.conn.HttpHostConnectException: Connection to http://ovm1:8889 refused
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:190)
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:645)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:480)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
    at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:283)
    ... 7 more
Caused by: java.net.ConnectException: Connection refused

我通过 Thread.sleep(5000); 阻止了这种情况的发生。我正在寻找 sleep 的更好替代品。睡眠总是假设时间长度为5秒。

您可以休眠直到建立连接。

 boolean up = false;
 while (!up) {
     try {
         // Try to connect
         up = true;
     } catch (Exception e) {
         Thread.sleep(5000);
     }
 }

在未来任何时候都可能成功的情况下使用的一种常用方法是 back off pattern,通常通过将每次迭代的等待时间加倍来实现。

像这样:

long wait = 50; // ms
boolean connected;
while (!connected) {
    Thread.sleep(wait);
    connected = <code to check connection>
    wait *= 2;
}