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
source share