For a while I used HttpClient in a multi-threaded environment. For each thread, when it initiates a connection, it will create a completely new instance of HttpClient.
I recently discovered that with this approach, a user can open too many ports, and most connections are in TIME_WAIT state.
http://www.opensubscriber.com/message/commons-httpclient-dev@jakarta.apache.org/86045.html
Therefore, instead of each thread, do:
HttpClient c = new HttpClient(); try { c.executeMethod(method); } catch(...) { } finally { method.releaseConnection(); }
We are planing:
[METHOD A]
// global_c is initialized once through // HttpClient global_c = new HttpClient(new MultiThreadedHttpConnectionManager()); try { global_c.executeMethod(method); } catch(...) { } finally { method.releaseConnection(); }
In a normal situation, global_c will be available simultaneously with 50 ++ threads. I was wondering if this would create any performance issues? Is MultiThreadedHttpConnectionManager using a locking mechanism to implement thread safety policies?
If 10 threads use global_c, will the remaining 40 threads be blocked?
Or would it be better if I created an HttpClient instance in each thread but explicitly freed up the connection manager?
[METHOD B]
MultiThreadedHttpConnectionManager connman = new MultiThreadedHttpConnectionManager(); HttpClient c = new HttpClient(connman); try { c.executeMethod(method); } catch(...) { } finally { method.releaseConnection(); connman.shutdown(); }
Will connman.shutdown () suffer from performance issues?
Can I find out which method (A or B) is better for an application using 50 ++ streams?
Cheok Yan Cheng Aug 15 '09 at 5:13 2009-08-15 05:13
source share