Search code examples
javaapache-httpclient-4.xapache-httpcomponents

Configuring RequestConfig and PoolingHttpClientConnectionManager


I'm implementing rest client with apache http client. So I'm configuring it with RequestConfig:

<bean id="rest-client.requestConfigBuilder" class="org.apache.http.client.config.RequestConfig"
      factory-method="custom">
    <property name="socketTimeout" value="${rest.readTimeout}"/>
    <property name="connectTimeout" value="${rest.connectTimeout}"/>
    <property name="connectionRequestTimeout" value="${rest.connectTimeout}"/>
</bean>

And with

<bean id="rest-client.connectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
    <property name="maxTotal" value="${rest.maxTotalConnections}"/>
    <property name="defaultMaxPerRoute" value="${rest.maxTotalConnections}"/>
</bean>

My question is - how do I pickup best parameters for timeouts, maxTotal connections, etc? Is there any guide? Because what I'm doing now - just randomly picking up those parameters. I think those values should be set reasonably. Can you please explain me how to chose them or point to good articles. And maybe are there some tools to profile this?


Solution

  • These 2 questions are good starting point (what are default setting on most browsers): Max parallel http connections in a browser? and Http client timeout and server timeout These parameters setting will greatly depending on the server load and network latency. If your client will be deploy on a particular network requesting several servers, you can measure these timeouts and determine the number of connections base no servers' limits and load.