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 vijay kolli <vi...@gmail.com> on 2011/12/01 23:22:12 UTC

set different socket timeout for same httpclient using MultiThreadedHttpConnectionManager

i have two threads using the same instance of the HTTPClientTest. two
threads call the send method on the same HTTPClientTest. how should i
set a different socket timeout for each of those threads that call the
send method. if i do something like this within the send method then
both threads executing the send method would have same socket timeout.
managerParams.setSoTimeout(60);connectionManager.setParams(managerParams);

how should i create a different socket timeout for multiple threads
executing the send method on the same instance of HTTPClientTest.

public class HTTPClientTest implements Runnable{
private HttpClient httpClient;
private MultiThreadedHttpConnectionManager connectionManager;
private HttpConnectionManagerParams managerParams;
private HttpClientTest()
{
     connectionManager = new MultiThreadedHttpConnectionManager();
     httpClient = new HttpClient(connectionManager);
}
public static synchronized HTTPClientTest getInstance()
{
    if(instance == null)
        instance = new HTTPClientTest();
    return instance;
}

public void send(String message, String url)
{
    PostMethod post = new PostMethod(url);
    String reply = "";
    String length = message.length() + "";
    post.setRequestHeader("Content-Length", length);
    try
    {
        System.out.println("HTTP request: " + message);
        StringRequestEntity postBody = new
StringRequestEntity(message, "text/xml", "UTF-8");
        post.setRequestEntity(postBody);
        int status = httpClient.executeMethod(post);
        System.out.println("HTTP status: " + status);
        reply = post.getResponseBodyAsString();
        System.out.println("HTTP Post response code: " + reply);

    }
    catch(HttpException e) {
        e.printStackTrace();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        post.releaseConnection();
    }

}
}

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


Re: set different socket timeout for same httpclient using MultiThreadedHttpConnectionManager

Posted by William Speirs <ws...@apache.org>.
The overhead is probably not great... give it a try and see if it works for
you. If not, you'll need to re-work your logic.

Bill-

On Fri, Dec 2, 2011 at 10:47 AM, vijay kolli
<vi...@gmail.com>wrote:

> I need to consider performance in my application. I am just doing a
> normal http connection nothing like via SSL. So what could be the
> overhead of creating a separate httpclient on each send call.
>
> On Fri, Dec 2, 2011 at 9:24 AM, William Speirs <ws...@apache.org> wrote:
> > Yea... I mentioned performance only because there is some overhead in
> > creating a new client, especially if you're talking to servers via SSL as
> > it needs to load the key store, etc.
> >
> > If this is not a high performance app, then you should be find with
> this...
> >
> > Bill-
> >
> > On Fri, Dec 2, 2011 at 9:55 AM, vijay kolli
> > <vi...@gmail.com>wrote:
> >
> >> So is it fine if we do something like this. This would end up creating
> >> a new http client for each method call. would this not effect any of
> >> the performance
> >>        public void send(String message, String url, int timeout)
> >>        {
> >>                 MultiThreadedHttpConnectionManager connectionManager
> >> =  new MultiThreadedHttpConnectionManager();
> >>                  HttpClient httpClient =  new
> >> HttpClient(connectionManager);
> >>                 HttpConnectionManagerParams managerParams;
> >>                managerParams.setSoTimeout(timeout);
> >>                connectionManager.setParams(managerParams);
> >>                 PostMethod post = new PostMethod(url);
> >>                String reply = "";
> >>                 try
> >>                 {
> >>                        String length = message.length() + "";
> >>                        post.setRequestHeader("Content-Length", length);
> >>                                 StringRequestEntity postBody = new
> >> StringRequestEntity(message,
> >> "text/xml", "UTF-8");
> >>                        post.setRequestEntity(postBody);
> >>                        int status = httpClient.executeMethod(post);
> >>                                 reply = post.getResponseBodyAsString();
> >>                 }
> >>
> >>        }
> >>
> >>
> >>
> >> On Thu, Dec 1, 2011 at 4:36 PM, William Speirs <ws...@apache.org>
> wrote:
> >> > Depending on you performance needs, you could make a new client in
> each
> >> > send call, and the set the socket timeout.
> >> >
> >> > Bill-
> >> > On Dec 1, 2011 5:22 PM, "vijay kolli" <vi...@gmail.com>
> >> wrote:
> >> >
> >> >> i have two threads using the same instance of the HTTPClientTest. two
> >> >> threads call the send method on the same HTTPClientTest. how should i
> >> >> set a different socket timeout for each of those threads that call
> the
> >> >> send method. if i do something like this within the send method then
> >> >> both threads executing the send method would have same socket
> timeout.
> >> >>
> >>
> managerParams.setSoTimeout(60);connectionManager.setParams(managerParams);
> >> >>
> >> >> how should i create a different socket timeout for multiple threads
> >> >> executing the send method on the same instance of HTTPClientTest.
> >> >>
> >> >> public class HTTPClientTest implements Runnable{
> >> >> private HttpClient httpClient;
> >> >> private MultiThreadedHttpConnectionManager connectionManager;
> >> >> private HttpConnectionManagerParams managerParams;
> >> >> private HttpClientTest()
> >> >> {
> >> >>     connectionManager = new MultiThreadedHttpConnectionManager();
> >> >>     httpClient = new HttpClient(connectionManager);
> >> >> }
> >> >> public static synchronized HTTPClientTest getInstance()
> >> >> {
> >> >>    if(instance == null)
> >> >>        instance = new HTTPClientTest();
> >> >>    return instance;
> >> >> }
> >> >>
> >> >> public void send(String message, String url)
> >> >> {
> >> >>    PostMethod post = new PostMethod(url);
> >> >>    String reply = "";
> >> >>    String length = message.length() + "";
> >> >>    post.setRequestHeader("Content-Length", length);
> >> >>    try
> >> >>    {
> >> >>        System.out.println("HTTP request: " + message);
> >> >>        StringRequestEntity postBody = new
> >> >> StringRequestEntity(message, "text/xml", "UTF-8");
> >> >>        post.setRequestEntity(postBody);
> >> >>        int status = httpClient.executeMethod(post);
> >> >>        System.out.println("HTTP status: " + status);
> >> >>        reply = post.getResponseBodyAsString();
> >> >>        System.out.println("HTTP Post response code: " + reply);
> >> >>
> >> >>    }
> >> >>    catch(HttpException e) {
> >> >>        e.printStackTrace();
> >> >>    }
> >> >>    catch(IOException e)
> >> >>    {
> >> >>        e.printStackTrace();
> >> >>    }
> >> >>    catch(Exception e)
> >> >>    {
> >> >>        e.printStackTrace();
> >> >>    }
> >> >>    finally
> >> >>    {
> >> >>        post.releaseConnection();
> >> >>    }
> >> >>
> >> >> }
> >> >> }
> >> >>
> >> >> ---------------------------------------------------------------------
> >> >> 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
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: set different socket timeout for same httpclient using MultiThreadedHttpConnectionManager

Posted by vijay kolli <vi...@gmail.com>.
I need to consider performance in my application. I am just doing a
normal http connection nothing like via SSL. So what could be the
overhead of creating a separate httpclient on each send call.

On Fri, Dec 2, 2011 at 9:24 AM, William Speirs <ws...@apache.org> wrote:
> Yea... I mentioned performance only because there is some overhead in
> creating a new client, especially if you're talking to servers via SSL as
> it needs to load the key store, etc.
>
> If this is not a high performance app, then you should be find with this...
>
> Bill-
>
> On Fri, Dec 2, 2011 at 9:55 AM, vijay kolli
> <vi...@gmail.com>wrote:
>
>> So is it fine if we do something like this. This would end up creating
>> a new http client for each method call. would this not effect any of
>> the performance
>>        public void send(String message, String url, int timeout)
>>        {
>>                 MultiThreadedHttpConnectionManager connectionManager
>> =  new MultiThreadedHttpConnectionManager();
>>                  HttpClient httpClient =  new
>> HttpClient(connectionManager);
>>                 HttpConnectionManagerParams managerParams;
>>                managerParams.setSoTimeout(timeout);
>>                connectionManager.setParams(managerParams);
>>                 PostMethod post = new PostMethod(url);
>>                String reply = "";
>>                 try
>>                 {
>>                        String length = message.length() + "";
>>                        post.setRequestHeader("Content-Length", length);
>>                                 StringRequestEntity postBody = new
>> StringRequestEntity(message,
>> "text/xml", "UTF-8");
>>                        post.setRequestEntity(postBody);
>>                        int status = httpClient.executeMethod(post);
>>                                 reply = post.getResponseBodyAsString();
>>                 }
>>
>>        }
>>
>>
>>
>> On Thu, Dec 1, 2011 at 4:36 PM, William Speirs <ws...@apache.org> wrote:
>> > Depending on you performance needs, you could make a new client in each
>> > send call, and the set the socket timeout.
>> >
>> > Bill-
>> > On Dec 1, 2011 5:22 PM, "vijay kolli" <vi...@gmail.com>
>> wrote:
>> >
>> >> i have two threads using the same instance of the HTTPClientTest. two
>> >> threads call the send method on the same HTTPClientTest. how should i
>> >> set a different socket timeout for each of those threads that call the
>> >> send method. if i do something like this within the send method then
>> >> both threads executing the send method would have same socket timeout.
>> >>
>> managerParams.setSoTimeout(60);connectionManager.setParams(managerParams);
>> >>
>> >> how should i create a different socket timeout for multiple threads
>> >> executing the send method on the same instance of HTTPClientTest.
>> >>
>> >> public class HTTPClientTest implements Runnable{
>> >> private HttpClient httpClient;
>> >> private MultiThreadedHttpConnectionManager connectionManager;
>> >> private HttpConnectionManagerParams managerParams;
>> >> private HttpClientTest()
>> >> {
>> >>     connectionManager = new MultiThreadedHttpConnectionManager();
>> >>     httpClient = new HttpClient(connectionManager);
>> >> }
>> >> public static synchronized HTTPClientTest getInstance()
>> >> {
>> >>    if(instance == null)
>> >>        instance = new HTTPClientTest();
>> >>    return instance;
>> >> }
>> >>
>> >> public void send(String message, String url)
>> >> {
>> >>    PostMethod post = new PostMethod(url);
>> >>    String reply = "";
>> >>    String length = message.length() + "";
>> >>    post.setRequestHeader("Content-Length", length);
>> >>    try
>> >>    {
>> >>        System.out.println("HTTP request: " + message);
>> >>        StringRequestEntity postBody = new
>> >> StringRequestEntity(message, "text/xml", "UTF-8");
>> >>        post.setRequestEntity(postBody);
>> >>        int status = httpClient.executeMethod(post);
>> >>        System.out.println("HTTP status: " + status);
>> >>        reply = post.getResponseBodyAsString();
>> >>        System.out.println("HTTP Post response code: " + reply);
>> >>
>> >>    }
>> >>    catch(HttpException e) {
>> >>        e.printStackTrace();
>> >>    }
>> >>    catch(IOException e)
>> >>    {
>> >>        e.printStackTrace();
>> >>    }
>> >>    catch(Exception e)
>> >>    {
>> >>        e.printStackTrace();
>> >>    }
>> >>    finally
>> >>    {
>> >>        post.releaseConnection();
>> >>    }
>> >>
>> >> }
>> >> }
>> >>
>> >> ---------------------------------------------------------------------
>> >> 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
>>
>>

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


Re: set different socket timeout for same httpclient using MultiThreadedHttpConnectionManager

Posted by William Speirs <ws...@apache.org>.
Yea... I mentioned performance only because there is some overhead in
creating a new client, especially if you're talking to servers via SSL as
it needs to load the key store, etc.

If this is not a high performance app, then you should be find with this...

Bill-

On Fri, Dec 2, 2011 at 9:55 AM, vijay kolli
<vi...@gmail.com>wrote:

> So is it fine if we do something like this. This would end up creating
> a new http client for each method call. would this not effect any of
> the performance
>        public void send(String message, String url, int timeout)
>        {
>                 MultiThreadedHttpConnectionManager connectionManager
> =  new MultiThreadedHttpConnectionManager();
>                  HttpClient httpClient =  new
> HttpClient(connectionManager);
>                 HttpConnectionManagerParams managerParams;
>                managerParams.setSoTimeout(timeout);
>                connectionManager.setParams(managerParams);
>                 PostMethod post = new PostMethod(url);
>                String reply = "";
>                 try
>                 {
>                        String length = message.length() + "";
>                        post.setRequestHeader("Content-Length", length);
>                                 StringRequestEntity postBody = new
> StringRequestEntity(message,
> "text/xml", "UTF-8");
>                        post.setRequestEntity(postBody);
>                        int status = httpClient.executeMethod(post);
>                                 reply = post.getResponseBodyAsString();
>                 }
>
>        }
>
>
>
> On Thu, Dec 1, 2011 at 4:36 PM, William Speirs <ws...@apache.org> wrote:
> > Depending on you performance needs, you could make a new client in each
> > send call, and the set the socket timeout.
> >
> > Bill-
> > On Dec 1, 2011 5:22 PM, "vijay kolli" <vi...@gmail.com>
> wrote:
> >
> >> i have two threads using the same instance of the HTTPClientTest. two
> >> threads call the send method on the same HTTPClientTest. how should i
> >> set a different socket timeout for each of those threads that call the
> >> send method. if i do something like this within the send method then
> >> both threads executing the send method would have same socket timeout.
> >>
> managerParams.setSoTimeout(60);connectionManager.setParams(managerParams);
> >>
> >> how should i create a different socket timeout for multiple threads
> >> executing the send method on the same instance of HTTPClientTest.
> >>
> >> public class HTTPClientTest implements Runnable{
> >> private HttpClient httpClient;
> >> private MultiThreadedHttpConnectionManager connectionManager;
> >> private HttpConnectionManagerParams managerParams;
> >> private HttpClientTest()
> >> {
> >>     connectionManager = new MultiThreadedHttpConnectionManager();
> >>     httpClient = new HttpClient(connectionManager);
> >> }
> >> public static synchronized HTTPClientTest getInstance()
> >> {
> >>    if(instance == null)
> >>        instance = new HTTPClientTest();
> >>    return instance;
> >> }
> >>
> >> public void send(String message, String url)
> >> {
> >>    PostMethod post = new PostMethod(url);
> >>    String reply = "";
> >>    String length = message.length() + "";
> >>    post.setRequestHeader("Content-Length", length);
> >>    try
> >>    {
> >>        System.out.println("HTTP request: " + message);
> >>        StringRequestEntity postBody = new
> >> StringRequestEntity(message, "text/xml", "UTF-8");
> >>        post.setRequestEntity(postBody);
> >>        int status = httpClient.executeMethod(post);
> >>        System.out.println("HTTP status: " + status);
> >>        reply = post.getResponseBodyAsString();
> >>        System.out.println("HTTP Post response code: " + reply);
> >>
> >>    }
> >>    catch(HttpException e) {
> >>        e.printStackTrace();
> >>    }
> >>    catch(IOException e)
> >>    {
> >>        e.printStackTrace();
> >>    }
> >>    catch(Exception e)
> >>    {
> >>        e.printStackTrace();
> >>    }
> >>    finally
> >>    {
> >>        post.releaseConnection();
> >>    }
> >>
> >> }
> >> }
> >>
> >> ---------------------------------------------------------------------
> >> 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: set different socket timeout for same httpclient using MultiThreadedHttpConnectionManager

Posted by vijay kolli <vi...@gmail.com>.
So is it fine if we do something like this. This would end up creating
a new http client for each method call. would this not effect any of
the performance
	public void send(String message, String url, int timeout)
	{
                 MultiThreadedHttpConnectionManager connectionManager
=  new MultiThreadedHttpConnectionManager();
                  HttpClient httpClient =  new HttpClient(connectionManager);
                 HttpConnectionManagerParams managerParams;
                managerParams.setSoTimeout(timeout);
                connectionManager.setParams(managerParams);
		PostMethod post = new PostMethod(url);
		String reply = "";
		try
		{
			String length = message.length() + "";
			post.setRequestHeader("Content-Length", length);
				StringRequestEntity postBody = new StringRequestEntity(message,
"text/xml", "UTF-8");
			post.setRequestEntity(postBody);
			int status = httpClient.executeMethod(post);
				reply = post.getResponseBodyAsString();
		}
		
	}



On Thu, Dec 1, 2011 at 4:36 PM, William Speirs <ws...@apache.org> wrote:
> Depending on you performance needs, you could make a new client in each
> send call, and the set the socket timeout.
>
> Bill-
> On Dec 1, 2011 5:22 PM, "vijay kolli" <vi...@gmail.com> wrote:
>
>> i have two threads using the same instance of the HTTPClientTest. two
>> threads call the send method on the same HTTPClientTest. how should i
>> set a different socket timeout for each of those threads that call the
>> send method. if i do something like this within the send method then
>> both threads executing the send method would have same socket timeout.
>> managerParams.setSoTimeout(60);connectionManager.setParams(managerParams);
>>
>> how should i create a different socket timeout for multiple threads
>> executing the send method on the same instance of HTTPClientTest.
>>
>> public class HTTPClientTest implements Runnable{
>> private HttpClient httpClient;
>> private MultiThreadedHttpConnectionManager connectionManager;
>> private HttpConnectionManagerParams managerParams;
>> private HttpClientTest()
>> {
>>     connectionManager = new MultiThreadedHttpConnectionManager();
>>     httpClient = new HttpClient(connectionManager);
>> }
>> public static synchronized HTTPClientTest getInstance()
>> {
>>    if(instance == null)
>>        instance = new HTTPClientTest();
>>    return instance;
>> }
>>
>> public void send(String message, String url)
>> {
>>    PostMethod post = new PostMethod(url);
>>    String reply = "";
>>    String length = message.length() + "";
>>    post.setRequestHeader("Content-Length", length);
>>    try
>>    {
>>        System.out.println("HTTP request: " + message);
>>        StringRequestEntity postBody = new
>> StringRequestEntity(message, "text/xml", "UTF-8");
>>        post.setRequestEntity(postBody);
>>        int status = httpClient.executeMethod(post);
>>        System.out.println("HTTP status: " + status);
>>        reply = post.getResponseBodyAsString();
>>        System.out.println("HTTP Post response code: " + reply);
>>
>>    }
>>    catch(HttpException e) {
>>        e.printStackTrace();
>>    }
>>    catch(IOException e)
>>    {
>>        e.printStackTrace();
>>    }
>>    catch(Exception e)
>>    {
>>        e.printStackTrace();
>>    }
>>    finally
>>    {
>>        post.releaseConnection();
>>    }
>>
>> }
>> }
>>
>> ---------------------------------------------------------------------
>> 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: set different socket timeout for same httpclient using MultiThreadedHttpConnectionManager

Posted by William Speirs <ws...@apache.org>.
Depending on you performance needs, you could make a new client in each
send call, and the set the socket timeout.

Bill-
On Dec 1, 2011 5:22 PM, "vijay kolli" <vi...@gmail.com> wrote:

> i have two threads using the same instance of the HTTPClientTest. two
> threads call the send method on the same HTTPClientTest. how should i
> set a different socket timeout for each of those threads that call the
> send method. if i do something like this within the send method then
> both threads executing the send method would have same socket timeout.
> managerParams.setSoTimeout(60);connectionManager.setParams(managerParams);
>
> how should i create a different socket timeout for multiple threads
> executing the send method on the same instance of HTTPClientTest.
>
> public class HTTPClientTest implements Runnable{
> private HttpClient httpClient;
> private MultiThreadedHttpConnectionManager connectionManager;
> private HttpConnectionManagerParams managerParams;
> private HttpClientTest()
> {
>     connectionManager = new MultiThreadedHttpConnectionManager();
>     httpClient = new HttpClient(connectionManager);
> }
> public static synchronized HTTPClientTest getInstance()
> {
>    if(instance == null)
>        instance = new HTTPClientTest();
>    return instance;
> }
>
> public void send(String message, String url)
> {
>    PostMethod post = new PostMethod(url);
>    String reply = "";
>    String length = message.length() + "";
>    post.setRequestHeader("Content-Length", length);
>    try
>    {
>        System.out.println("HTTP request: " + message);
>        StringRequestEntity postBody = new
> StringRequestEntity(message, "text/xml", "UTF-8");
>        post.setRequestEntity(postBody);
>        int status = httpClient.executeMethod(post);
>        System.out.println("HTTP status: " + status);
>        reply = post.getResponseBodyAsString();
>        System.out.println("HTTP Post response code: " + reply);
>
>    }
>    catch(HttpException e) {
>        e.printStackTrace();
>    }
>    catch(IOException e)
>    {
>        e.printStackTrace();
>    }
>    catch(Exception e)
>    {
>        e.printStackTrace();
>    }
>    finally
>    {
>        post.releaseConnection();
>    }
>
> }
> }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>