I am using RestTemplate
with HttpComponentsClientHttpRequestFactory
as shown below:
private RestTemplate restTemplate = new RestTemplate();
// singleton class so only one instance
public DataProcess() {
restTemplate.setRequestFactory(clientHttpRequestFactory());
}
private ClientHttpRequestFactory clientHttpRequestFactory() {
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(1000).setConnectTimeout(1000)
.setSocketTimeout(1000).setStaleConnectionCheckEnabled(false).build();
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
poolingHttpClientConnectionManager.setMaxTotal(300);
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(300);
CloseableHttpClient httpClientBuilder = HttpClientBuilder.create()
.setConnectionManager(poolingHttpClientConnectionManager).setMaxConnPerRoute(300).setMaxConnTotal(300)
.setDefaultRequestConfig(requestConfig).build();
requestFactory.setHttpClient(httpClientBuilder);
return requestFactory;
}
Now my question is: What's the difference between setting setMaxTotal
and setDefaultMaxPerRoute
in PoolingHttpClientConnectionManager
object versus setting setMaxConnPerRoute
and setMaxConnTotal
in CloseableHttpClient
. Do I need to set these at both the places?
Also are there any other settings which we should use with RestTemplate? I am calling Restful service by using RestTemplate as my HttpClient so it has to be fast because this code will be running in a multithreading application.
The builder parameters are used to construct a default pooling connection manager if you don't provide one. So in your example the sets on the builder are redundant.
See HttpClientBuilder (sorry I had to snip this enormous method down a lot):