21

I am trying to hit a server using HTTP client using PoolingClientConnectionManager setting max connections for individual hosts

//Code that initializes my connection manager and HTTP client 
HttpParams httpParam = httpclient.getParams();
HttpConnectionParams.setSoTimeout(httpParam, SOCKET_TIMEOUT);

HttpConnectionParams.setConnectionTimeout(httpParam, CONN_TIMEOUT);

httpclient.setParams(httpParam);

//Run a thread which closes Expired connections
new ConnectionManager(connManager).start(); 

//Code that executes my request 
HttpPost httpPost = new HttpPost(url);
        HttpEntity httpEntity = new StringEntity(request, "UTF-8");
httpPost.setEntity(httpEntity);

Header acceptEncoding = new BasicHeader("Accept-Encoding", "gzip,deflate");
httpPost.setHeader(acceptEncoding);     

if(contenttype != null && !contenttype.equals("")){
    Header contentType = new BasicHeader("Content-Type", contenttype);
    httpPost.setHeader(contentType);
}
InputStream inputStream = null;
LOG.info(dataSource + URL + url + REQUEST + request);

HttpResponse response = httpclient.execute(httpPost);

That is we are using Connection pooling for http persistence .

We are getting this error sporadically :

The target server failed to respond
org.apache.http.NoHttpResponseException: The target server failed to respond
        at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:95)
        at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:62)
        at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254)
        at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289)
        at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252)
        at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:191)
        at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300)
        at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:517)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)

Does any one know how to resolve this?

We are shutting down idle connections as well.

Can some Please help.

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html

5
  • Post your client initialization and request execution code, please.
    – vzamanillo
    Mar 17, 2015 at 17:06
  • //Code that inilizes my connection mananger and http client HttpParams httpParam = httpclient.getParams(); HttpConnectionParams.setSoTimeout(httpParam, SOCKET_TIMEOUT); HttpConnectionParams.setConnectionTimeout(httpParam, CONN_TIMEOUT); httpclient.setParams(httpParam); //Run a thread which closes Expired connections new ConnectionManager(connManager).start();
    – Balaji V
    Mar 18, 2015 at 7:41
  • //Code that executes my request HttpPost httpPost = new HttpPost(url); HttpEntity httpEntity = new StringEntity(request, "UTF-8"); httpPost.setEntity(httpEntity); Header acceptEncoding = new BasicHeader("Accept-Encoding", "gzip,deflate"); httpPost.setHeader(acceptEncoding); if(contenttype != null && !contenttype.equals("")){ Header contentType = new BasicHeader("Content-Type", contenttype); httpPost.setHeader(contentType); } InputStream inputStream = null; HttpResponse response = httpclient.execute(httpPost);
    – Balaji V
    Mar 18, 2015 at 7:47
  • hi i ve pasted the code snippet please help me thanks
    – Balaji V
    Mar 18, 2015 at 7:48
  • It looks like this bug still exists! Did you ever resolve your issue?
    – Arya
    Sep 30, 2016 at 5:16

2 Answers 2

18

Probably, it is a bug in the HttpClient.

If you are using the HttpClient 4.4, please try to upgrade to 4.4.1.

If you want for more information, please look at this link.

If you can't upgrade, the following links might be helpful.

http://www.nuxeo.com/blog/using-httpclient-properly-avoid-closewait-tcp-connections/

Good luck!

1
  • 7
    I am using 4.4.1, Unfortunately I have the same issue.
    – arjuncc
    Oct 16, 2017 at 14:03
9

Recently faced similar while using HttpClient 5.

On enabling the HttpClient logs and found that the issue was due to stale connections.

Adding the below helped to solve the issue, it detects and validates the connections that have become stale while kept inactive in the pool before reuse.

PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();

connectionManager.setValidateAfterInactivity(timeinmilliseconds);
1
  • 6
    What value have you used for timeinmilliseconds ?
    – alexanoid
    Aug 22, 2018 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.