What does setDefaultMaxPerRoute and setMaxTotal mean in HttpClient?

I am using Apache HttpClient in one of my projects. I also use PoolingHttpClientConnectionManager along with my HttpClient.

I am confused what these properties mean. I tried to look at the documentation in the code, but I do not see any documentation around these variables, so I could not understand.

  • setMaxTotal
  • setDefaultMaxPerRoute
  • setConnectTimeout
  • setSocketTimeout
  • setConnectionRequestTimeout
  • setStaleConnectionCheckEnabled

The following is an example of using my code:

 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5 * 1000).setSocketTimeout(5 * 1000) .setStaleConnectionCheckEnabled(false).build(); PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(); poolingHttpClientConnectionManager.setMaxTotal(200); poolingHttpClientConnectionManager.setDefaultMaxPerRoute(20); CloseableHttpClient httpClientBuilder = HttpClientBuilder.create() .setConnectionManager(poolingHttpClientConnectionManager).setDefaultRequestConfig(requestConfig) .build(); 

Can someone explain these properties to me so that I can understand and decide what values ​​I should put there. Also, are there any other properties that I should use separately, as shown above, to improve performance?

I am using http-client 4.3.1

+8
source share
1 answer

Some options are explained at http://hc.apache.org/httpclient-3.x/preference-api.html

Others must be gleaned from the source.

  • setMaxTotal

The maximum number of connections allowed for all routes.

  • setDefaultMaxPerRoute

The maximum number of connections allowed for a route that was not specified otherwise by calling setMaxPerRoute. Use setMaxPerRoute when you know the route ahead of time and setDefaultMaxPerRoute when you do not.

  • setConnectTimeout

How long to wait for a connection to be established with a remote server before throwing a timeout exception.

  • setSocketTimeout

How long to wait for the server to respond to various calls before throwing a timeout exception. See http://docs.oracle.com/javase/1.5.0/docs/api/java/net/SocketOptions.html#SO_TIMEOUT for details.

  • setConnectionRequestTimeout

How long to wait when you try to check the connection from the connection pool before throwing an exception (the connection pool will not immediately return if, for example, all connections are deleted).

  • setStaleConnectionCheckEnabled

May be disabled for small performance improvements due to potential IOExceptions. See http://hc.apache.org/httpclient-3.x/performance.html#Stale_connection_check

+6
source

All Articles