1

我正在使用带有 PoolingHttpClientConnectionManager 的 HttpClient 4.3 和两个线程,每个线程都连接到 REST API。两者都在永久发送请求。它在几分钟内运行良好,但随后请求开始超时。浏览时,即使 Chrome 也无法建立与网站的连接。这是我发送请求的代码:

String sendGETApache(HttpGet httpGet) {
    try {
        CloseableHttpResponse response = httpClient.execute(httpGet); //, httpContext);
        HttpEntity entity = null;
        try {
            entity = response.getEntity();
            if(entity != null) {
                String s = EntityUtils.toString(entity);
                return s;
            }
        }   catch (Exception e) {
            e.printStackTrace();
            httpGet.abort();
        } finally {
            EntityUtils.consume(entity);
            response.close();
        }
    } catch (Exception e) {
        httpGet.abort();
        e.printStackTrace();
    } finally {
        httpGet.releaseConnection();
    }
    return "";
}

HttpClient 是一个独特的对象,在两个线程之间共享。我已经尝试将 DefaultMaxPerRoute 和 MaxTotal 限制为 1 和 2,我什至使用了非常大的值,但没有结果。我什至实现了一个额外的线程,它反复调用 closeExpiredConnections() 和 closeIdleConnections() ,也没有任何改进。你知道我能做什么吗?我认为某处资源正在泄漏,但我不知道在哪里......谢谢

4

0 回答 0