我们不应该在 Jest Http Client for elasticsearch 响应后关闭连接吗

Should we not be closing the connection after response in JestHttpClient for elastic search

这是关于代码的 https://github.com/searchbox-io/Jest/blob/master/jest/src/main/java/io/searchbox/client/http/JestHttpClient.java

在此代码段中

public <T extends JestResult> T execute(Action<T> clientRequest) throws IOException {
    HttpUriRequest request = prepareRequest(clientRequest);
    HttpResponse response = httpClient.execute(request);

    return deserializeResponse(response, request, clientRequest);
}

private <T extends JestResult> T deserializeResponse(HttpResponse response, Action<T> clientRequest) throws IOException {
    StatusLine statusLine = response.getStatusLine();
    return clientRequest.createNewElasticSearchResult(
            response.getEntity() == null ? null : EntityUtils.toString(response.getEntity()),
            statusLine.getStatusCode(),
            statusLine.getReasonPhrase(),
            gson
    );
}

收到回复后我们不应该做类似

的事情吗
response.close()

这个特定的堆栈溢出线程 提到使用

消耗响应实体
EntityUtils.consume(HttpEntity)

只做 EntityUtils.toString(response.getEntity()) 就够了吗?

EntityUtils.toString(response.getEntity())只要能顺利完成就够了。例如,如果抛出 UnsupportedEncodingException,响应将不会被 toString 读取并且连接将被阻止。我的建议是在 finally 块中调用 EntityUtils.consume(HttpEntity) 以防止挂起连接。