|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
HttpClient instance managementDear 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@... For additional commands, e-mail: httpclient-users-help@... |
|
|
Re: HttpClient instance managementOn 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@... > For additional commands, e-mail: httpclient-users-help@... > --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-users-unsubscribe@... For additional commands, e-mail: httpclient-users-help@... |
|
|
Re: HttpClient instance managementThank 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@... >> For additional commands, e-mail: httpclient-users-help@... >> > > --------------------------------------------------------------------- > To unsubscribe, e-mail: httpclient-users-unsubscribe@... > For additional commands, e-mail: httpclient-users-help@... > 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@... |
|
|
Re: HttpClient instance managementOn 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@... >>> For additional commands, e-mail: httpclient-users-help@... >>> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: httpclient-users-unsubscribe@... >> For additional commands, e-mail: httpclient-users-help@... >> > > 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@... > > > > --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-users-unsubscribe@... For additional commands, e-mail: httpclient-users-help@... |
| Free embeddable forum powered by Nabble | Forum Help |