Java – should the connectmanager really shut down?
Sometimes I get
java.net.socketException: Too many open files java.net.socket.createImpl(Socket.java:397) java.net.socket.connect(Socket.java:527) org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:123) org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:123) org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:147) org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:108) org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415) org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641) org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:597)
I saw a similar problem Java net. Socketexception: too many open files. I also use Apache's httpclient, but it seems that the answer is not very helpful to me
There are two answers:
1) Do httpclient getConnectionManager(). shutdown();
2) Calling entity getContent(). Close() instead of entity consumeContent()
But none seems to fit
1) The problem is that I'm using threadsafeconnectionmanager It is created once (at application startup) Therefore, we will not close (so that the connection can be reused) If I close this manager – all connections will be closed If I use single client connection manager, it is appropriate, but I don't Am I right?
2) I found that I didn't turn off the stream either But when I started debugging - it looked like the getcontent () stream was closed, even before consumecontent () was called Although (even after consumecontent) the socketinputstream in those getcontent () streams is not closed, and the socket Isn't that good? Could it be the cause of the problem? I haven't found a way to turn off this socket! It's inside the external input stream, so I can't get it But I see in debug mode that the socket is not closed and socketinputstream
How should we normally use threadsafeconnectionmanager – should it be created only once? If so, how to turn off these sockets correctly?
Solution
@H_ 403_ 33@
If I do shutdown on this manager – all the connection will be closed. It would be appropriate if I use SingleClientConnectionManager,but I don’t. Am I right?
You should use only one connection manager instance for each different HTTP service You don't actually need to close it, but you may want to evict connections that have been idle for more than a given period of time from the pool, as follows:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e659
Although (even after consumeContent) the socketInputStream inside those getContent() stream was not closed,as well as socket. Is it bad?
No, it's not If the underlying connection is in a consistent state, it can still remain active However, the content stream must be closed to ensure that the connection is released back to the connection manager
How normally we should work with a ThreadSafeConnectionManager – should it be created only once? and if it is so,how to properly close those sockets if any?
Yes, it should be created only once However, you must ensure that the connection is properly released back to the manager by closing the response content stream You may also want to actively evict connections from the pool after a period of inactivity
Finally, note that other components of your application may also leak file descriptors The culprit may not necessarily be the connection manager of httpclient