On Tue, Feb 15, 2011 at 3:06 PM, Oleg Kalnichevski <[email protected]> wrote: > > Well, I finally spotted an obvious problem with your code, which I > should have found earlier: HttpRequest objects are NOT thread-safe. They > may not be used by multiple threads. > > Oleg >
Hi Oleg, By HttpRequest you mean HttpClient? If so then I believe it's a documentation bug because the code is mostly based on this sample: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e650. Also it's perhaps wise to document that fact that the class is not thread safe in the javadoc's. I've shared my test code https://gist.github.com/829061, could you perhaps run it on your machine? It's and eclipse project with m2eclipse support configured. As for the code, I've made the modifications but the behavior remains: Starting thread execution: Wed Feb 16 12:18:11 IST 2011 Thread-1: Exception Wed Feb 16 12:18:33 IST 2011 did a total of 10277 get requests Thread-2: Exception Wed Feb 16 12:18:34 IST 2011 did a total of 10549 get requests Thread-3: Exception Wed Feb 16 12:18:36 IST 2011 did a total of 11590 get requests // MAXIM: 3 threads on breakpoint at: e.printStackTrace(); // MAXIM: Hitting F8 on breakpoint of Thread-2 org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection Thread-2: Abort Wed Feb 16 12:21:31 IST 2011 Thread-2: Exception Wed Feb 16 12:21:31 IST 2011 did a total of 104835 get requests at org.apache.http.impl.conn.tsccm.ConnPoolByRoute.getEntryBlocking(ConnPoolByRoute.java:417) at org.apache.http.impl.conn.tsccm.ConnPoolByRoute$1.getPoolEntry(ConnPoolByRoute.java:300) at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager$1.getConnection(ThreadSafeClientConnManager.java:224) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:391) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754) at HTTPClientTest$GetThread.run(HTTPClientTest.java:75) Feb 16, 2011 12:21:31 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute INFO: I/O exception (java.net.SocketException) caught when processing request: Socket closed Feb 16, 2011 12:21:31 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute INFO: Retrying request Thread-4: Exception Wed Feb 16 12:21:31 IST 2011 did a total of 104835 get requests What is interesting here it that once Thread-2 is released to do the httpGet.abort() the 4'th thread (in this case Thread-4) also stops. >From this situation the system does not recover - It does not matter how many times I allow the threads to run - No more GET events arrive at maxim-lp (I'm watching apache logs there). Also please note the I'm monitoring the number of connections to maxim-lp using lsof -i -P and can confirm that in no point more then 1 connection was open, so I think no connections are being leaked. I would appreciate your help to research this further, this starts to get interesting :). Maxim. p.s. In the mean time I've implemented a different solution, based on ThreadLocal. I'm ok with thread local for now because I will always be talking to the same host, each ThreadLocal<HttpClient> holds a completely separated HttpClient and because HttpClient by default uses persistent connection to the same RoutePath it works for me. (if anyone is interesting please email me and i'll share the code with you). --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
