作者为什么使用EntityUtils.consume(httpEntity);? [英] Why did the author use EntityUtils.consume(httpEntity);?

查看:63
本文介绍了作者为什么使用EntityUtils.consume(httpEntity);?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过 EntityUtils.consume(httpEntity); 但我不确定它到底是做什么的.

I've come across EntityUtils.consume(httpEntity); and I'm not sure what it really does.

例如:

try {

    //... some code

    HttpEntity httpEntity = httpResponse.getEntity();
    BufferedReader br = new BufferedReader(new InputStreamReader(http.Entity.getContent()));
    String line;
    while ((line = br.readLine())!= null) {
        System.out.println(line);
    }
    EntityUtils.consume(httpEntity);
} catch (Exception e) {
    //code
} finally { 
    httpClient.getConnectionManager().shutdown();
}

作者为什么要在 EntityUtils.consume(httpEntity);finally 块将关闭连接并且垃圾收集器将负责 httpEntity?

Why did the author put in EntityUtils.consume(httpEntity); when the finally block will close the connection and garbage collector will take care of httpEntity?

推荐答案

归根结底就是做一个好公民"(并且真正了解 HTTPClient 接口的契约).EntityUtils.consume 将释放 httpEntity 持有的所有资源,这实质上意味着释放任何底层 Stream 并将 Connection 对象返回到其池(在这种情况下连接管理器是一个多线程的)或释放连接管理器以便它可以处理下一个请求.

It really boils down to being a "good citizen" (and really knowing the contracts of HTTPClient interfaces). What EntityUtils.consume will do is release all resources held by the httpEntity, which essentially implies releasing any underlying Stream and giving the Connection object back to its pool (in the case your connection manager is a multithreaded one) or freeing the connection manager so that it can process the next request.

如果您不使用 entity,会发生什么实际上取决于 finally 子句中关闭连接管理器"的含义.它会关闭尚未发送回池的待处理流/连接吗?我不确定它会在合同上做到这一点(尽管我认为在实施方面确实如此).如果没有,那么您可能正在泄漏系统资源(套接字等).发生的事情还取决于 Entity 对象的可能终结方法,该方法可能(如果它被执行)释放其资源,同样,不确定它是否在实体的合同中这样做.

If you do not consume the entity, what happens really depends on what "shutting down the connection manager" means in the finally clause. Will it close pending streams / connections that have not been sent back to the pool? I'm not sure it will contractually do that (although implementation-wise I think it does). If it does not, then you may be leaking system resources (sockets etc.). What happens can also depend on a possible finalization method of the Entity object that may (if it gets executed at all) release its resources, again, not sure it is in the entity's contract to do that.

让我们假设 ConnectionManager 实际上在关闭时优雅地关闭所有挂起的资源.你还需要消耗实体吗?我说是的,因为一个月后,有人会修改您的代码并在同一个 try/finally 块中进行第二次 HTTP 调用,并且可能无法这样做,因为您没有按照应有的方式释放资源(例如,如果您的客户端位于单个连接池中,不释放第一个连接将使第二个调用失败).

Let's suppose for a minute that the ConnectionManager actually closes all pending resources gracefully when it shuts down. Would you still need to consume the Entity? I say yes, because one month from now, someone will modify your code and make a second HTTP call in the same try/finally block, and may be unable to do so because you have not freed resources the way you should have (e.g. if you client is on a single connection pool, not freeing the first connection will make a second call fail).

所以我的观点是:实体就是资源,资源在不需要的时候应该被释放.指望别人在以后为您释放它们可能会在将来伤害您.原作者可能是这么想的.

So my point is : Entities are resources, and resources should be freed when they are not needed. Counting on others to free them for you at a later point may hurt you in the future. The original author may have thought along those lines.

作为旁注,请注意您编写的实现实际上会在底层流的末尾消耗读者,因此消耗调用实际上什么都不做,但在我看来,这是一个实现细节(出乎我的意料,一旦响应流被完全读取,连接对象就会自动释放/发送回 http 客户端中的池).另请注意,如果您使用 API 提供的 ResponseHandler 机制,所有这些消费逻辑也会从您那里抽象出来.最后,API 不保证 response.getEntity 永远不会返回 null,因此您应该检查以避免 NullPointerException.

As a side note, notice that the implementation you wrote will actually consume the reader up to the end of the underlying stream, so the consume call will actually do nothing at all, but in my opinion, this is an implementation detail (out of the top of my head, once a response stream as been completely read, the connection object is automatically released / sent back to the pool in http client). Note also that all this Consume logic is also abstracted away from you if you use the ResponseHandler mecanism the API offers. Finally, the API does not guarantee that response.getEntity will never return null, so you should check that to avoid NullPointerException.

这篇关于作者为什么使用EntityUtils.consume(httpEntity);?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆