Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does setDefaultMaxPerRoute and setMaxTotal mean in HttpClient?

I am using Apache HttpClient in one of my project. I am also using PoolingHttpClientConnectionManager along with my HttpClient as well.

I am confuse what are these properties mean. I tried going through documentation in the code but I don't see any documentation around these variables so was not able to understand.

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

Below is how I am using in 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 anyone explain me these properties so that I can understand and decide what values I should put in there. Also, are there any other properties that I should use apart from as shown above to get better performance?

I am using http-client 4.3.1

like image 370
john Avatar asked Jun 07 '15 04:06

john


People also ask

What is setDefaultMaxPerRoute?

setDefaultMaxPerRoute(int max) – Set the maximum number of concurrent connections per route, which is two by default. setMaxPerRoute(int max) – Set the total number of concurrent connections to a specific route, which is two by default.

What is Max connections per route?

By default, the maximum number of connections is 20 and the maximum connection number per route is 2. However, these values are generally too low for real-world applications. For example, when all the connections are busy with handling other requests, HttpClient won't create a new connection if the number exceeds 20.

What is stale connection in HttpClient?

Stale connection checkHTTP specification permits both the client and the server to terminate a persistent (keep-alive) connection at any time without notice to the counterpart, thus rendering the connection invalid or stale.

What is the use of PoolingHttpClientConnectionManager?

PoolingHttpClientConnectionManager is a more complex implementation that manages a pool of client connections and is able to service connection requests from multiple execution threads. Connections are pooled on a per route basis.


1 Answers

Some parameters 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 across all routes.

  • setDefaultMaxPerRoute

The maximum number of connections allowed for a route that has not been specified otherwise by a call to 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 the 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 trying to checkout a connection from the connection pool before throwing an exception (the connection pool won't return immediately if, for example, all the connections are checked out).

  • setStaleConnectionCheckEnabled

Can be disabled for a slight performance improvement at the cost of potential IOExceptions. See http://hc.apache.org/httpclient-3.x/performance.html#Stale_connection_check

like image 142
Pace Avatar answered Sep 20 '22 11:09

Pace