如何在commons-httpclient中强制断开连接?
我使用 commons-httpclient 将查询发送到 Web 服务器。 Web 服务器从不关闭连接,但存在与连接上下文相关的小内存泄漏。
因此,我想时不时地关闭持久连接(例如,每个 X 查询),但我没有找到任何方法来做到这一点。我使用 MultiThreadedHTTPConnectionManager
我可以使用 MultiThreadedHTTPConnectionManager.closeIdleConnections(very_small_delay),但这不是很可靠:如果我的 Java 代码有很多工作要做,那么可能没有连接空闲,因为总是有其他线程在等待空闲联系。
谢谢,
I use commons-httpclient to send queries to a Web server. The Web server never closes connections, but has a small memory leak associated to the connection context.
I would therefore like to close the persistent connections from time to time (every X queries for example), but I did not find any way to do this. I use the MultiThreadedHTTPConnectionManager
I could use MultiThreadedHTTPConnectionManager.closeIdleConnections(very_small_delay), but that's not very reliable: if my Java code has much work to do, then it's possible that no connection is ever idle, because there are always other threads waiting for a free connection.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
执行一个方法后,假设是 GET,您可以读取响应的 HttpStatus 编号,以决定是否需要调用 method.releaseConnection() 。
或者,如果您知道要关闭哪个连接,您可以尝试
一下,我认为您有某种原因想要“杀死”连接。假设您拥有要关闭的连接(即 HttpConnection 对象)。这个方法有帮助吗?
after executing one method, let's say GET, you can read the responsed HttpStatus number, to decide if a method.releaseConnection() needs to be called.
Or if you know which connection you wanna close, you could try
okay, I thought you have some reason that you want to 'kill' a connection. Assume you have the connection, the HttpConnection Object, which you want to close in hand. May this method help?
您正在调用
method.releaseConnection()
吗?http://hc.apache.org/httpclient-3.x/ threading.html#Connection_Release
Are you calling
method.releaseConnection()
?http://hc.apache.org/httpclient-3.x/threading.html#Connection_Release
您能负担得起使用
SimpleHttpConnectionManager(boolean alwaysClose)
的费用吗?Can you afford to use
SimpleHttpConnectionManager(boolean alwaysClose)
?当我使用
releaseConnections()
时,仍然有连接处于CLOSE_WAIT
状态。如果我在releaseConnections
之后添加closeIdleConnections(0)
可以吗?这是否意味着即使连接被发送回池以供其他客户端使用但未关闭/释放,那么 closeIdleConnections 也会关闭它?
When I am using
releaseConnections()
still there are connections inCLOSE_WAIT
state. Is it okay if I addcloseIdleConnections(0)
afterreleaseConnections
?Will this mean even if the connection is sent back to the pool to be used by other clients but is not closed/released then
closeIdleConnections
will close it?