setMaxTotal and setDefaultMaxPerRoute in HttpClient?

6.1k views Asked by At

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.

1

There are 1 answers

7
roby On BEST ANSWER

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):

 public CloseableHttpClient build() {
    ... snip ...
    HttpClientConnectionManager connManagerCopy = this.connManager;
    if (connManagerCopy == null) {
        ... snip ...
        @SuppressWarnings("resource")
        final PoolingHttpClientConnectionManager poolingmgr = new PoolingHttpClientConnectionManager(
                RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("https", sslSocketFactoryCopy)
                    .build(),
                null,
                null,
                null,
                connTimeToLive,
                connTimeToLiveTimeUnit != null ? connTimeToLiveTimeUnit : TimeUnit.MILLISECONDS);
        ... snip ...
        if (maxConnTotal > 0) {
            poolingmgr.setMaxTotal(maxConnTotal);
        }
        if (maxConnPerRoute > 0) {
            poolingmgr.setDefaultMaxPerRoute(maxConnPerRoute);
        }
        connManagerCopy = poolingmgr;
    }
    ... snip ...
    return new InternalHttpClient(... snip ...);
}