java 关闭 CloseableHttpResponse/CloseableHttpClient 的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48928930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 10:12:48  来源:igfitidea点击:

Right way to close CloseableHttpResponse/CloseableHttpClient

javaapache-httpclient-4.xresource-cleanupautocloseable

提问by user7294900

I'm using CloseableHttpResponse (from apache-httpclient-4.5.3) and I'm not sure I'm using it right, I saw an answer with no votesto use EntityUtils.consumeon finally:

我使用CloseableHttpResponse(来自Apache的HttpClient的-4.5.3),我不知道我使用的是正确的,我看到了一个不带票的答案EntityUtils.consume最后在:

CloseableHttpResponse response1 = httpclient.execute(httpGet);

try {

 System.out.println(response1.getStatusLine());

} finally {

 EntityUtils.consume(response1.getEntity());
CloseableHttpResponse response1 = httpclient.execute(httpGet);

try {

 System.out.println(response1.getStatusLine());

} finally {

 EntityUtils.consume(response1.getEntity());

CloseableHttpClientis abstract and has no close method to call although in this answerit's used:

CloseableHttpClient是抽象的,没有 close 方法可以调用,尽管在这个答案中使用了它:

CloseableHttpResponse response = httpclient.execute(httpget);
try {
    //do something
} finally {
    response.close();
}

Currently I'm using try with resources for CloseableHttpClientand CloseableHttpResponseinside of send method.

目前我正在使用 tryCloseableHttpClientCloseableHttpResponsesend 方法内部的资源。

Am I not missing any resource open or using it in a wrong way?

我没有错过任何打开的资源或以错误的方式使用它吗?

private CloseableHttpResponse send()
            throws URISyntaxException, UnsupportedEncodingException, IOException, ClientProtocolException {
        URIBuilder uriBuilder = new URIBuilder(BASE_URL);
        HttpHost target = new HttpHost(uriBuilder.getHost(), uriBuilder.getPort(), uriBuilder.getScheme());
        HttpPost post = new HttpPost(uriBuilder.build());
        try (CloseableHttpClient httpClient = HttpClients.custom().build(); CloseableHttpResponse response = httpClient.execute(target, post)) {            
            return response;
        }

采纳答案by atoMerz

It has been explained in detail in the docs here.
Quoting the pseudo code from the docs here's a typical way to allocate/deallocate an instance of CloseableHttpClient:

它已在此处的文档中详细解释。
从这里引用文档中的伪代码是分配/取消分配实例的典型方法CloseableHttpClient

try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
    <...>
}

The same applies to CloseableHttpResponse:

这同样适用于CloseableHttpResponse

try (CloseableHttpResponse response = httpclient.execute(httpget)) {
    <...>
}

Now, about the close method in CloseableHttpClient. CloseableHttpClientis an abstract class that implements Closeableinterface. That is, although it doesn't have a closemethod itself the classes that extend it are required to implement the closemethod. One class is InternalHttpClient. You can check the source code for the details.

现在,关于CloseableHttpClient. CloseableHttpClient是一个实现Closeable接口的抽象类。也就是说,虽然它本身没有close方法,但需要扩展它的类来实现该close方法。一类是InternalHttpClient。您可以查看源代码以了解详细信息。

Before Java7, explicit close would be required:

在 Java7 之前,需要显式关闭:

CloseableHttpClient httpclient = HttpClients.createDefault();
try {
    <...>
} finally {
    httpclient.close();
}

CloseableHttpResponse response = httpclient.execute(httpget);
try {
    <...>
} finally {
    response.close();
}

回答by dvallejo

You can avoid the finally by using the try(resource)

您可以通过使用 try(resource) 来避免 finally

try (CloseableHttpResponse response = httpclient.execute(httpGet)) { ... }

尝试 (CloseableHttpResponse 响应 = httpclient.execute(httpGet)) { ... }