Hello.
I am trying to configure HttpClient to validate a list of URLs and I don't
want it to take longer than 10 seconds for each URL. I have been searching
for all available timeout settings in HttpClient but, even after setting
them all to 2 seconds, HttpClient still takes longer than 10 seconds for
some URLs.
This is an example test case that configures all known timeouts to 2
seconds but the HttpClient execution takes about 15 seconds:
SocketConfig socketConfig = SocketConfig.copy(SocketConfig.DEFAULT)
.setSoTimeout(Timeout.ofSeconds(2))
.build();
ConnectionConfig connectionConfig =
ConnectionConfig.copy(ConnectionConfig.DEFAULT)
.setConnectTimeout(2, TimeUnit.SECONDS)
.setSocketTimeout(2, TimeUnit.SECONDS)
.build();
PoolingHttpClientConnectionManager connManager =
PoolingHttpClientConnectionManagerBuilder.create()
.setDefaultSocketConfig(socketConfig)
.setDefaultConnectionConfig(connectionConfig)
.build();
RequestConfig defaultRequestConfig = RequestConfig.copy(RequestConfig.DEFAULT)
.setConnectionRequestTimeout(2, TimeUnit.SECONDS)
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(connManager)
.setDefaultRequestConfig(defaultRequestConfig)
.build();
ClassicHttpRequest httpGet = ClassicRequestBuilder
.head("https://www.anek.gr/en/")
.build();
Instant start = Instant.now();
httpClient.execute(httpGet, response -> {
System.out.println(response.getCode() + " " + response.getReasonPhrase());
final HttpEntity entity1 = response.getEntity();
EntityUtils.consume(entity1);
return null;
});
Instant end = Instant.now();
long diff = ChronoUnit.SECONDS.between(start, end);
System.out.println("It takes " + diff + " seconds.");
Am I missing a timeout setting? Are there missing knobs in HttpClient
configuration?
Thanks!
Óscar