How to force disconnect in commons-httpclient?

I use commons-httpclient to send requests to the web server. The web server never closes the connection, but has a small memory leak associated with the connection context.

Therefore, I would like to close persistent connections from time to time (for example, every X request), but I have not found a way to do this. I am using MultiThreadedHTTPConnectionManager

I could use MultiThreadedHTTPConnectionManager.closeIdleConnections (very_small_delay), but it is not very reliable: if my Java code has a lot of work, it is possible that no connection will ever work, because there are always other threads waiting for a free connection.

Thanks,

+4
source share
4 answers

after executing one method, say GET, you can read the HttpStatus response number to decide whether to call the .releaseConnection () method.

Or, if you know which connection you want to close, you can try

MultiThreadedHTTPConnectionManager void releaseConnection(HttpConnection conn) 

Well, I thought you had a reason why you want to “kill” the connection. Suppose you have a connection, the HttpConnection object that you want to close. Can this method help?

 HttpConnection public void close() Closes the socket and streams. 
+2
source
+1
source

Can you use SimpleHttpConnectionManager(boolean alwaysClose) ?

 httpClient.setHttpConnectionManager(new SimpleHttpConnectionManager(true)) 
0
source

When I use releaseConnections() , there are still connections in the CLOSE_WAIT state. Is it possible to add closeIdleConnections(0) after releaseConnections ?

Will this mean, even if the connection is sent back to the pool, which will be used by other clients, but not closed / released, then closeIdleConnections will close it?

0
source

Source: https://habr.com/ru/post/1300289/


All Articles