如何在commons-httpclient中强制断开连接?

发布于 2024-08-20 05:35:17 字数 327 浏览 7 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感悟人生的甜 2024-08-27 05:35:17

执行一个方法后,假设是 GET,您可以读取响应的 HttpStatus 编号,以决定是否需要调用 method.releaseConnection() 。

或者,如果您知道要关闭哪个连接,您可以尝试

MultiThreadedHTTPConnectionManager
void    releaseConnection(HttpConnection conn) 

一下,我认为您有某种原因想要“杀死”连接。假设您拥有要关闭的连接(即 HttpConnection 对象)。这个方法有帮助吗?

HttpConnection
public void close()

    Closes the socket and streams. 

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

MultiThreadedHTTPConnectionManager
void    releaseConnection(HttpConnection conn) 

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?

HttpConnection
public void close()

    Closes the socket and streams. 
鲜血染红嫁衣 2024-08-27 05:35:17

您能负担得起使用 SimpleHttpConnectionManager(boolean alwaysClose) 的费用吗?

httpClient.setHttpConnectionManager(new SimpleHttpConnectionManager(true))

Can you afford to use SimpleHttpConnectionManager(boolean alwaysClose)?

httpClient.setHttpConnectionManager(new SimpleHttpConnectionManager(true))
油焖大侠 2024-08-27 05:35:17

当我使用 releaseConnections() 时,仍然有连接处于 CLOSE_WAIT 状态。如果我在 releaseConnections 之后添加 closeIdleConnections(0) 可以吗?

这是否意味着即使连接被发送回池以供其他客户端使用但未关闭/释放,那么 closeIdleConnections 也会关闭它?

When I am using releaseConnections() still there are connections in CLOSE_WAIT state. Is it okay if I add closeIdleConnections(0) after releaseConnections?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文