在检查Netty服务器是否启动时睡眠的替代方案?

时间:2016-02-29 13:32:03

标签: java netty resteasy

我正在使用休息界面启动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秒。

2 个答案:

答案 0 :(得分:2)

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

这样的事情:

xWinCount = document.getElementById('xWinCount');

答案 1 :(得分:1)

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

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