Jetty HttpClient ExecutionException与InterruptedException

时间:2019-05-20 20:30:57

标签: java network-programming jetty-9

我正在使用Jetty向服务器发出获取请求,并在请求失败时重试, 但是我对异常处理及其解释有一些疑问。

代码如下所示

public void run() {
    final int MAX_CONNECTION_WAIT_TIME = 90;
    final long requestTimeOut = 2L;
    long sleep = 1L;
    do {
        final HttpClient httpClient = new HttpClient();
        try {
            httpClient.start();
            final String resp = httpClient
                .newRequest(socket)
                .timeout(requestTimeOut, TimeUnit.SECONDS)
                .send().getContentAsString();
             //do something with the resp
            } catch (TimeoutException toe) {
                //what it means?
            } catch (ExecutionException ee) {
                //what it means?
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
            } finally {
                httpClient.stop();
            }
            //wait until the next time
            try {
                TimeUnit.SECONDS.sleep(sleep);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
        }
    } while (sleep < MAX_CONNECTION_WAIT_TIME);
}

如果请求失败(可能是间歇性的网络问题),我想重试一次,但是不会永远。

如果发生上述情况

  • 哪个例外会首先发生?
  • TimeoutException是否暗示ExecutionException?
  • 如果请求失败,是否将引发ExecutionException或TimeoutException?

文档说

InterruptedException – if send thread is interrupted
java.util.concurrent.TimeoutException – if send times out
java.util.concurrent.ExecutionException – if execution fails

现在我要做的是忽略TimeoutException并捕获ExecutionException,它表现良好,是正确的方法还是我缺少了什么?

0 个答案:

没有答案