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

查看:15020
本文介绍了为什么笔者使用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); 最后块将关闭连接和垃圾收集器会照顾 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 ,基本上意味着释放任何基础流,并给予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.

如果你不消耗实体,会发生什么情况实际上取决于最终条款中什么是关闭连接管理器的意思。它将关闭那些没有被送回池中待定流/连接?我不相信这将合同做(尽管实现明智的,我认为它)。如果没有,那么你可能会泄漏系统资源(插座等)。
什么情况还取决于实体对象的一个​​可能的完成方法可能(如果它得到所有执行)再次释放其资源,不知道这是在实体的合同要做到这一点。

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 实际上关闭所有未决的资源正常,当它关闭一分钟。你仍然需要消耗的实体?我说是的,因为从现在一个月时间,有人会修改code,并在同一个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客户端发送回池)。
另请注意,这一切消耗逻辑也抽象离你而去,如果你使用ResponseHandler所mecanism的API优惠。
最后,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天全站免登陆