0

这是关于 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()

这个特殊的堆栈溢出线程HttpClient 4.0.1 - 如何释放连接?提到使用响应实体

EntityUtils.consume(HttpEntity)

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

4

1 回答 1

1

EntityUtils.toString(response.getEntity())如果它能够成功完成就足够了。例如,如果抛出 UnsupportedEncodingException,则不会读取响应toString并阻止连接。我的建议是调用块EntityUtils.consume(HttpEntity)finally防止挂起连接。

于 2016-03-21T12:56:20.117 回答