Some correction in Method B. It should be :
[METHOD B]
MultiThreadedHttpConnectionManager connman = new
MultiThreadedHttpConnectionManager();
HttpClient c = new HttpClient(connman);
try {
c.executeMethod(method);
}
catch(...) {
}
finally {
method.releaseConnection();
connman.shutdown();
}
Thanks and Regards
Yan Cheng Cheok
--- On Sun, 8/16/09, Yan Cheng Cheok <[email protected]> wrote:
> From: Yan Cheng Cheok <[email protected]>
> Subject: Best Practice to Use HttpClient in Multithreaded Environment
> To: [email protected]
> Date: Sunday, August 16, 2009, 9:11 PM
> Hi all,
>
> All the while, I am using HttpClient in multithreaded
> environment. For every threads, when they initiate a
> connection, they will create a complete new HttpClient
> instance.
>
> Recently, I discover, by using this approach, it can cause
> the user is having too many port being opened, and most of
> the connections are in TIME_WAIT state.
>
> http://www.opensubscriber.com/message/[email protected]/86045.html
>
> Hence, instead of per thread doing :
> HttpClient c = new HttpClient();
> try {
> c.executeMethod(method);
> }
> catch(...) {
> }
> finally {
> method.releaseConnection();
> }
>
>
> We plan to have :
>
> [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 normal situation, global_c will be accessed by 50++
> threads concurrently. I was wondering, whether this will
> occur any performance issue? Is
> MultiThreadedHttpConnectionManager using lock-free mechanism
> to implement its thread safe policy?
>
> It is possible if 10 threads are using global_c, will the
> other 40 threads being locked?
>
> Or will it better if in every threads, I create a instance
> for every HttpClient, but release the connection manager
> explicitly.
>
> [METHOD B]
> HttpClient c = new HttpClient();
> try {
> c.executeMethod(method);
> }
> catch(...) {
> }
> finally {
> method.releaseConnection();
> c.getHttpConnectionManager().shutdown();
> }
>
> Is c.getHttpConnectionManager().shutdown() suffer
> performance issues?
>
> May I know which method (A or B) is better, for application
> using 50++ threads?
>
> I am using HttpClient 3.1
>
> Thanks and Regards
> Yan Cheng Cheok
>
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]