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 Imre Fazekas <Fa...@connexis.com> on 2009/06/24 15:45:51 UTC

HttpClient instance management

Dear All,


i'm just wondering when the DefaultHttpClient class should be  
instantiated.
I have a class providing basic HTTP services like sending JSON  
message, sending a get and parsing the response, etc. So this class  
has static methods.
I defined these members with these initializations:
private static HttpParams                  defaultParameters;
private static  SchemeRegistry              supportedSchemes;
private static  ClientConnectionManager     clcm;
static{
         setup();
         clcm = createManager();
     }

     private static final void setup() {
         supportedSchemes = new SchemeRegistry();
         SocketFactory sf = PlainSocketFactory.getSocketFactory();
         supportedSchemes.register(new Scheme("http", sf, 80));

         HttpParams params = new BasicHttpParams( );
         HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
         HttpProtocolParams.setUseExpectContinue(params, false);

         defaultParameters = params;

         context = new BasicHttpContext( );
     }

     private static final ClientConnectionManager createManager() {
         return new ThreadSafeClientConnManager( defaultParameters,  
supportedSchemes );
     }


May i define the HttpClient as a static field or i should define  
always a new instance when a service method is called? Like this:
public static Response get(String uri) {
         DefaultHttpClient httpClient = getHttpClient();
         HttpGet get = new HttpGet( serverURI + uri );
	...
}



Thank you in advance!

Regards,

Imre





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


Re: HttpClient instance management

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, Jun 24, 2009 at 04:24:39PM +0200, Imre Fazekas wrote:
> Thank you for the fast reply:
>
> A lot of get,post requests go through these methods, and after having  
> defined the httpclient static sometimes i receive this warning:
> "2009.06.24. 15:23:29 org.apache.http.impl.conn.SingleClientConnManager 
> revokeConnection
> WARNING: Invalid use of SingleClientConnManager: connection still  
> allocated.
> Make sure to release the connection before allocating another one."
>
> Do you have any idea what to do?
>
>

You should be using ThreadSafeClientConnManager instead of
SingleClientConnManager. For details see the section on connection management
in the HttpClient tutorial:

http://wiki.apache.org/HttpComponents/HttpClientTutorial

Oleg 


> Regards,
>
> Imre
>
>
> On 2009.06.24., at 15:57, Oleg Kalnichevski wrote:
>
>> On Wed, Jun 24, 2009 at 03:45:51PM +0200, Imre Fazekas wrote:
>>> Dear All,
>>>
>>>
>>> i'm just wondering when the DefaultHttpClient class should be
>>> instantiated.
>>> I have a class providing basic HTTP services like sending JSON  
>>> message,
>>> sending a get and parsing the response, etc. So this class has static
>>> methods.
>>> I defined these members with these initializations:
>>> private static HttpParams                  defaultParameters;
>>> private static  SchemeRegistry              supportedSchemes;
>>> private static  ClientConnectionManager     clcm;
>>> static{
>>>        setup();
>>>        clcm = createManager();
>>>    }
>>>
>>>    private static final void setup() {
>>>        supportedSchemes = new SchemeRegistry();
>>>        SocketFactory sf = PlainSocketFactory.getSocketFactory();
>>>        supportedSchemes.register(new Scheme("http", sf, 80));
>>>
>>>        HttpParams params = new BasicHttpParams( );
>>>        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
>>>        HttpProtocolParams.setUseExpectContinue(params, false);
>>>
>>>        defaultParameters = params;
>>>
>>>        context = new BasicHttpContext( );
>>>    }
>>>
>>>    private static final ClientConnectionManager createManager() {
>>>        return new ThreadSafeClientConnManager( defaultParameters,
>>> supportedSchemes );
>>>    }
>>>
>>>
>>> May i define the HttpClient as a static field or i should define  
>>> always a
>>> new instance when a service method is called? Like this:
>>
>> You may and you should. In short one should have:
>>
>> 1 HttpClient per service / application
>> 1 HttpContext per thread / user
>> 1 HttpRequest per request execution
>>
>> Oleg
>>
>>
>>> public static Response get(String uri) {
>>>        DefaultHttpClient httpClient = getHttpClient();
>>>        HttpGet get = new HttpGet( serverURI + uri );
>>>      ...
>>> }
>>>
>>>
>>>
>>> Thank you in advance!
>>>
>>> Regards,
>>>
>>> Imre
>>>
>>>
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> 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
>>
>
> Imre Fazekas
> Connexis Kft.
> 4034 Debrecen, V?g?h?d u. 2.
> Office:  +36 52 887 500 / 8789
> Cell:      +36-70-514 8550
> Fax:      +36 52 887 505
> Email: fazekas@connexis.com
>
>
>
>

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


Re: HttpClient instance management

Posted by Imre Fazekas <fa...@connexis.com>.
Thank you for the fast reply:

A lot of get,post requests go through these methods, and after having  
defined the httpclient static sometimes i receive this warning:
"2009.06.24. 15:23:29  
org.apache.http.impl.conn.SingleClientConnManager revokeConnection
WARNING: Invalid use of SingleClientConnManager: connection still  
allocated.
Make sure to release the connection before allocating another one."

Do you have any idea what to do?


Regards,

Imre


On 2009.06.24., at 15:57, Oleg Kalnichevski wrote:

> On Wed, Jun 24, 2009 at 03:45:51PM +0200, Imre Fazekas wrote:
>> Dear All,
>>
>>
>> i'm just wondering when the DefaultHttpClient class should be
>> instantiated.
>> I have a class providing basic HTTP services like sending JSON  
>> message,
>> sending a get and parsing the response, etc. So this class has static
>> methods.
>> I defined these members with these initializations:
>> private static HttpParams                  defaultParameters;
>> private static  SchemeRegistry              supportedSchemes;
>> private static  ClientConnectionManager     clcm;
>> static{
>>        setup();
>>        clcm = createManager();
>>    }
>>
>>    private static final void setup() {
>>        supportedSchemes = new SchemeRegistry();
>>        SocketFactory sf = PlainSocketFactory.getSocketFactory();
>>        supportedSchemes.register(new Scheme("http", sf, 80));
>>
>>        HttpParams params = new BasicHttpParams( );
>>        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
>>        HttpProtocolParams.setUseExpectContinue(params, false);
>>
>>        defaultParameters = params;
>>
>>        context = new BasicHttpContext( );
>>    }
>>
>>    private static final ClientConnectionManager createManager() {
>>        return new ThreadSafeClientConnManager( defaultParameters,
>> supportedSchemes );
>>    }
>>
>>
>> May i define the HttpClient as a static field or i should define  
>> always a
>> new instance when a service method is called? Like this:
>
> You may and you should. In short one should have:
>
> 1 HttpClient per service / application
> 1 HttpContext per thread / user
> 1 HttpRequest per request execution
>
> Oleg
>
>
>> public static Response get(String uri) {
>>        DefaultHttpClient httpClient = getHttpClient();
>>        HttpGet get = new HttpGet( serverURI + uri );
>>      ...
>> }
>>
>>
>>
>> Thank you in advance!
>>
>> Regards,
>>
>> Imre
>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> 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
>

Imre Fazekas
Connexis Kft.
4034 Debrecen, Vágóhíd u. 2.
Office:  +36 52 887 500 / 8789
Cell:      +36-70-514 8550
Fax:      +36 52 887 505
Email: fazekas@connexis.com





Re: HttpClient instance management

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, Jun 24, 2009 at 03:45:51PM +0200, Imre Fazekas wrote:
> Dear All,
>
>
> i'm just wondering when the DefaultHttpClient class should be  
> instantiated.
> I have a class providing basic HTTP services like sending JSON message, 
> sending a get and parsing the response, etc. So this class has static 
> methods.
> I defined these members with these initializations:
> private static HttpParams                  defaultParameters;
> private static  SchemeRegistry              supportedSchemes;
> private static  ClientConnectionManager     clcm;
> static{
>         setup();
>         clcm = createManager();
>     }
>
>     private static final void setup() {
>         supportedSchemes = new SchemeRegistry();
>         SocketFactory sf = PlainSocketFactory.getSocketFactory();
>         supportedSchemes.register(new Scheme("http", sf, 80));
>
>         HttpParams params = new BasicHttpParams( );
>         HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
>         HttpProtocolParams.setUseExpectContinue(params, false);
>
>         defaultParameters = params;
>
>         context = new BasicHttpContext( );
>     }
>
>     private static final ClientConnectionManager createManager() {
>         return new ThreadSafeClientConnManager( defaultParameters,  
> supportedSchemes );
>     }
>
>
> May i define the HttpClient as a static field or i should define always a 
> new instance when a service method is called? Like this:

You may and you should. In short one should have:

1 HttpClient per service / application
1 HttpContext per thread / user
1 HttpRequest per request execution

Oleg


> public static Response get(String uri) {
>         DefaultHttpClient httpClient = getHttpClient();
>         HttpGet get = new HttpGet( serverURI + uri );
> 	...
> }
>
>
>
> Thank you in advance!
>
> Regards,
>
> Imre
>
>
>
>
>
> ---------------------------------------------------------------------
> 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