You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Ben Short <be...@benshort.co.uk> on 2010/12/16 15:42:36 UTC

Using an instance of HttpGet/HttpPost across multiple threads

Hi,

Will I run into any problems with the following code? Specifically
using the httpGet in more than one thread?


// might pass in a ThreadSafeClientConnManager here
        final DefaultHttpClient httpclient = new DefaultHttpClient();

        List<HttpHost> hosts = new ArrayList<HttpHost>();
        hosts.add(new HttpHost("www.host1.com"));
        hosts.add(new HttpHost("www.host2.com"));
        hosts.add(new HttpHost("www.host3.com"));

        String request = "/path/to/something?name=value&name1=value1";

        final HttpGet httpGet = new HttpGet(request);
        // set some headers here.

        for ( final HttpHost host : hosts ) {
            taskExecutor.execute(new Runnable() {
                public void run() {

                    try {
                        HttpResponse response =
httpclient.execute(host, httpGet);
                        // do what every with the response

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            });
        }

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Using an instance of HttpGet/HttpPost across multiple threads

Posted by Ben Short <be...@benshort.co.uk>.
To answer my own question no the HttpGet is not threadsafe.

On 16 December 2010 16:41, Ben Short <be...@benshort.co.uk> wrote:
> Hi,
>
> I found some information about Multithreaded request execution for
> HTTPClient version 4 here [1]
>
> My question is...
>
> Is it ok to use the HttpGet in many threads at once?
>
> Regards
>
> Ben
>
>
> [1] http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e629
>
> On 16 December 2010 15:37, Andy <an...@hotmail.com> wrote:
>>
>> Im doing something similar except Im using the MultiThreadedHttpConnectionManager.
>>
>> http://hc.apache.org/httpclient-3.x/threading.html
>>
>> Doing that you should be safe.  Just remember to set the options described in the above link.
>>
>> Andy
>>
>>
>>
>>> Date: Thu, 16 Dec 2010 14:42:36 +0000
>>> Subject: Using an instance of HttpGet/HttpPost across multiple threads
>>> From: ben@benshort.co.uk
>>> To: httpclient-users@hc.apache.org
>>>
>>> Hi,
>>>
>>> Will I run into any problems with the following code? Specifically
>>> using the httpGet in more than one thread?
>>>
>>>
>>> // might pass in a ThreadSafeClientConnManager here
>>>         final DefaultHttpClient httpclient = new DefaultHttpClient();
>>>
>>>         List<HttpHost> hosts = new ArrayList<HttpHost>();
>>>         hosts.add(new HttpHost("www.host1.com"));
>>>         hosts.add(new HttpHost("www.host2.com"));
>>>         hosts.add(new HttpHost("www.host3.com"));
>>>
>>>         String request = "/path/to/something?name=value&name1=value1";
>>>
>>>         final HttpGet httpGet = new HttpGet(request);
>>>         // set some headers here.
>>>
>>>         for ( final HttpHost host : hosts ) {
>>>             taskExecutor.execute(new Runnable() {
>>>                 public void run() {
>>>
>>>                     try {
>>>                         HttpResponse response =
>>> httpclient.execute(host, httpGet);
>>>                         // do what every with the response
>>>
>>>                     } catch (IOException e) {
>>>                         e.printStackTrace();
>>>                     }
>>>
>>>                 }
>>>             });
>>>         }
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>>> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>>>
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Using an instance of HttpGet/HttpPost across multiple threads

Posted by Ben Short <be...@benshort.co.uk>.
Hi,

I found some information about Multithreaded request execution for
HTTPClient version 4 here [1]

My question is...

Is it ok to use the HttpGet in many threads at once?

Regards

Ben


[1] http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e629

On 16 December 2010 15:37, Andy <an...@hotmail.com> wrote:
>
> Im doing something similar except Im using the MultiThreadedHttpConnectionManager.
>
> http://hc.apache.org/httpclient-3.x/threading.html
>
> Doing that you should be safe.  Just remember to set the options described in the above link.
>
> Andy
>
>
>
>> Date: Thu, 16 Dec 2010 14:42:36 +0000
>> Subject: Using an instance of HttpGet/HttpPost across multiple threads
>> From: ben@benshort.co.uk
>> To: httpclient-users@hc.apache.org
>>
>> Hi,
>>
>> Will I run into any problems with the following code? Specifically
>> using the httpGet in more than one thread?
>>
>>
>> // might pass in a ThreadSafeClientConnManager here
>>         final DefaultHttpClient httpclient = new DefaultHttpClient();
>>
>>         List<HttpHost> hosts = new ArrayList<HttpHost>();
>>         hosts.add(new HttpHost("www.host1.com"));
>>         hosts.add(new HttpHost("www.host2.com"));
>>         hosts.add(new HttpHost("www.host3.com"));
>>
>>         String request = "/path/to/something?name=value&name1=value1";
>>
>>         final HttpGet httpGet = new HttpGet(request);
>>         // set some headers here.
>>
>>         for ( final HttpHost host : hosts ) {
>>             taskExecutor.execute(new Runnable() {
>>                 public void run() {
>>
>>                     try {
>>                         HttpResponse response =
>> httpclient.execute(host, httpGet);
>>                         // do what every with the response
>>
>>                     } catch (IOException e) {
>>                         e.printStackTrace();
>>                     }
>>
>>                 }
>>             });
>>         }
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


RE: Using an instance of HttpGet/HttpPost across multiple threads

Posted by Andy <an...@hotmail.com>.
Im doing something similar except Im using the MultiThreadedHttpConnectionManager.

http://hc.apache.org/httpclient-3.x/threading.html

Doing that you should be safe.  Just remember to set the options described in the above link.  

Andy



> Date: Thu, 16 Dec 2010 14:42:36 +0000
> Subject: Using an instance of HttpGet/HttpPost across multiple threads
> From: ben@benshort.co.uk
> To: httpclient-users@hc.apache.org
> 
> Hi,
> 
> Will I run into any problems with the following code? Specifically
> using the httpGet in more than one thread?
> 
> 
> // might pass in a ThreadSafeClientConnManager here
>         final DefaultHttpClient httpclient = new DefaultHttpClient();
> 
>         List<HttpHost> hosts = new ArrayList<HttpHost>();
>         hosts.add(new HttpHost("www.host1.com"));
>         hosts.add(new HttpHost("www.host2.com"));
>         hosts.add(new HttpHost("www.host3.com"));
> 
>         String request = "/path/to/something?name=value&name1=value1";
> 
>         final HttpGet httpGet = new HttpGet(request);
>         // set some headers here.
> 
>         for ( final HttpHost host : hosts ) {
>             taskExecutor.execute(new Runnable() {
>                 public void run() {
> 
>                     try {
>                         HttpResponse response =
> httpclient.execute(host, httpGet);
>                         // do what every with the response
> 
>                     } catch (IOException e) {
>                         e.printStackTrace();
>                     }
> 
>                 }
>             });
>         }
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>