【问题标题】:Should we not be closing the connection after response in JestHttpClient for elastic search我们是否应该在 Jest HttpClient 响应弹性搜索后关闭连接
【发布时间】:2016-03-17 20:38:14
【问题描述】:

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

在这段代码中sn -p

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 - how to release connection? 提到了使用响应实体

EntityUtils.consume(HttpEntity)

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

【问题讨论】:

    标签: java apache-httpclient-4.x elasticsearch-jest


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-24
      • 2014-06-18
      相关资源
      最近更新 更多