My RestTemplate can only maintain two connections at a time. The 3rd request will be blocked until previous request(s) return.
I think something is wrong with RestTemplate bean configuration. Following is the configuration for RestTemplate:
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(socketFactory)
.build();
HttpComponentsClientHttpRequestFactory factory =
new HttpComponentsClientHttpRequestFactory(httpClient);
return new RestTemplate(factory);
I can't figure out why. Please help.
Check this Baeldung guide where it says:
The default size of the pool of concurrent connections that can be open by the manager is two for each route or target host
But you can modify that property:
PoolingHttpClientConnectionManager connManager
= new PoolingHttpClientConnectionManager();
connManager.setMaxTotal(5);
connManager.setDefaultMaxPerRoute(4);
Where:
setMaxTotal(int max)
– Set the maximum number of total open connectionssetDefaultMaxPerRoute(int max)
– Set the maximum number of concurrent connections per route, which is two by default.So you can inject the connManager
configuration into httpClient
:
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(connManager)
.setSSLSocketFactory(socketFactory)
.build();