Search code examples
javaapache-httpclient-4.xconnection-timeout

How to set timeout for each request using Apache httpclient 4.* with PoolingHttpClientConnectionManager?


How to use the Apache httpclient 4.3 and PoolingHttpClientConnectionManager and pass the ConnectTimeOut and ReadTimeOut for each request.

For example, If I have the CloseableHttpClient as Singleton and using the PoolingHttpClientConnection to obtain the connection, for each request I make , I want to send different timeout values based on the target host

ie. HostA 10 Seconds , Host B 5 seconds etc

Please advise.


Solution

  • HttpGet get1 = new HttpGet("http://hosta/");
    RequestConfig config1 = RequestConfig.custom().setSocketTimeout(10000).build();
    get1.setConfig(config1);
    HttpGet get2 = new HttpGet("http://hostb/");
    RequestConfig config2 = RequestConfig.custom().setSocketTimeout(5000).build();
    get2.setConfig(config2);