Apache HttpClient 4.5: Reset connection

I am using httpClient version 4.5 to connect to our external provider site. We don't need connection pooling or persistent connection, so I use BasicHttpClientConnectionManager to create HttpClient.

This is great for the minimum number of requests, but if I test this at 1TPS for 1 hour, then by the end of the test we will start looking at intermittent connections. (number of guess requests> 100)

I / O exception (java.net.SocketException) encountered while processing request {s} -> https: // apiURL: 443 : Connection reset

Below is a snippet of code to connect.

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), new X509TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1.2" }, null,SSLConnectionSocketFactory.getDefaultHostnameVerifier());
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("https", sslsf).register("http", new PlainConnectionSocketFactory()).build();

HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(socketFactoryRegistry);
HttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(1, false);

RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(Integer.parseInt(30000)).setConnectTimeout(Integer.parseInt(30000)).setConnectionRequestTimeout(30000).setCookieSpec(CookieSpecs.STANDARD).build();

CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).setDefaultRequestConfig(defaultRequestConfig).setRetryHandler(retryHandler).evictExpiredConnections().build();

HttpPost httpPost = new HttpPost(<endpoint>);
httpPost.setEntity(new UrlEncodedFormEntity(requestData));
httpResponse = httpClient.execute(httpPost);

      

I saw that a fix for a similar problem is already available with version 4.5. (Link: https://issues.apache.org/jira/browse/HTTPCLIENT-1655 ) provided by Oleg

If so, not sure why I am still facing this problem. Can someone help with this.

Thank!

+3


source to share


1 answer


Hi Oleg ,

I was using httpclient version 4.5.3 when I was still seeing reset connection errors as above.

We later noticed that a fix for the reset issue was made for version 4.5.1 ( https://issues.apache.org/jira/browse/HTTPCLIENT-1655 ). So, just trying to update that particular version ran the test and didn't see any reset connection errors again. I expected this fix to also be available in higher versions starting from 4.5.1. But I guess it was somehow missed in higher versions, confirmed to be still a problem with version 4.5.3.



So the conclusion is that the reset connection error has been fixed using the httpclient 4.5.1 jar.

Thank!

+2


source







All Articles