user16655
user16655

Reputation: 1941

Edit timeout in HttpClient (4.5)

I need to be able to modify the timeout set in my CloseAbleHttpClient. Here's how I set the different timeouts:

RequestConfig config = RequestConfig.copy(RequestConfig.DEFAULT)
            .setConnectTimeout(timeout)
            .setSocketTimeout(timeout)
            .setConnectionRequestTimeout(managerTimeout)
            .build();

    httpClient = HttpClients.custom()
            .setConnectionManager(connectionManager)
            .setDefaultRequestConfig(config)
            .build();

Do I have to create a new RequestConfig object, and build the HttpClients.custom() again to achieve this? I have tried this, and the problem arises in that I need my CloseableHttpClient to be final. If I set the timeout 2 times, this isn't possible. I would appreciate any input on how to best modify the timout properties!

Upvotes: 3

Views: 8479

Answers (1)

ok2c
ok2c

Reputation: 27593

There is nothing that stops your from using different request config on a per request basis

RequestConfig defaultRequestConfig = RequestConfig.custom()
        .setConnectTimeout(timeout)
        .setSocketTimeout(timeout)
        .setConnectionRequestTimeout(managerTimeout)
        .build();

CloseableHttpClient httpClient = HttpClients.custom()
        .setConnectionManager(connectionManager)
        .setDefaultRequestConfig(defaultRequestConfig)
        .build();

RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
        .setConnectTimeout(timeout * 2)
        .setSocketTimeout(timeout * 2)
        .setConnectionRequestTimeout(managerTimeout * 2)
        .build();

HttpGet get = new HttpGet();
get.setConfig(requestConfig);

Upvotes: 11

Related Questions