Apache HTTPClient does not make more than two connections

I am trying to implement a connection pool for my application using Apache HTTPClient (v4.1). The problem is that the client always makes only two connections at startup, although there are enough threads running in parallel. I am trying to change the code for a while, but so far nothing has helped. I use ThreadSafeClientConnManager for the connection pool and set the MaxTotal and DefaulMaxPerRoute values ​​for the values ​​I want.
Is there anything that comes to your mind before I want to check?

Here is the code segment that I use to create the client.

 DefaultHttpClient createClient() { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, new Integer(60000)); params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, new Integer(60000)); params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("https", sf, 6443)); registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, registry); cm.setMaxTotal(2 * maxConnections); cm.setDefaultMaxPerRoute(maxConnections); HttpHost localhost = new HttpHost("localhost"); cm.setMaxForRoute(new HttpRoute(localhost), maxConnections); HttpHost sdpTargetHost = new HttpHost("webserviceIP", webservicePort, "https"); cm.setMaxForRoute(new HttpRoute(sdpTargetHost, null, true), maxConnections); return new DefaultHttpClient(cm, params); } 

The client returned by this function is used in Runnables managed by ThreadPoolExecutor . Runnables use the client and have the following lines:

 HttpResponse response = httpClient.execute(httpPost, context); HttpEntity entity = response.getEntity(); .... EntityUtils.consume(entity); 

From what I know, EntityUtils.consume(entity) will notify the connection manager that the connection is no longer in use, and thus release the connection that will be used by other threads. Therefore, I assume that connection management is fine.

I guess I have provided enough information, please tell me if I will add anything else.
Thanks

+4
source share
2 answers

OK I found a solution thanks to oleg for specifying logging, as well as for google and all forums.
All I had to do was define a class with only a connection manager, and then install HttpParams using HttpClient.setParams (). Therefore, the code will look something like this:

 DefaultHttpClient createClient() { SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("https", sf, 6443)); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(registry); cm.setMaxTotal(maxConnections); cm.setDefaultMaxPerRoute(maxConnections); HttpHost targetHost = new HttpHost("webserviceIP", webservicePort, "https"); cm.setMaxForRoute(new HttpRoute(targetHost, null, true), maxConnections); return new DefaultHttpClient(cm); } 

And right before using the client

 DefaultHttpClient httpClient = createClient(); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, new Integer(60000)); params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, new Integer(60000)); params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true); httpClient.setParams(params); 

It seems that the logic does not make a difference in the code, but this fixed my problem. I assume this is probably some bug in the HttpClient 4.1 API.

+7
source

I was unable to set this parameter to cm

 cm.setDefaultMaxPerRoute(maxConnections); 

This had to be done:

 ConnPerRoute perRoute = new ConnPerRouteBean(100); ConnManagerParams.setMaxConnectionsPerRoute(params, perRoute); ConnManagerParams.setMaxTotalConnections(params, 100); ConnManagerParams.setTimeout(params, 15000); 
0
source

All Articles