How to force disconnection in commons-httpclient?
-
25-09-2019 - |
문제
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,
해결책
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.
다른 팁
Are you calling method.releaseConnection()
?
http://hc.apache.org/httpclient-3.x/threading.html#Connection_Release
Can you afford to use SimpleHttpConnectionManager(boolean alwaysClose)
?
httpClient.setHttpConnectionManager(new SimpleHttpConnectionManager(true))
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?