No setSocketTimeout method in org.apache.hc.client5.http.config.RequestConfig.Builder

394 views Asked by At

I am migrating some projects from java 11 and Spring Boot 2.x to Java 17 and Spring Boot 3.0.0. I have the following code:

        return org.apache.http.impl.client.HttpClients.custom()
                .setKeepAliveStrategy(httpClient4connectionKeepAliveStrategy)
                .setConnectionManager(httpClient4ConnectionManager(withCustomSslConfig))
                .setDefaultRequestConfig(org.apache.http.client.config.RequestConfig.custom()
                        .setConnectionRequestTimeout(clientConnection.getConnectionRequestTimeout())
                        .setConnectTimeout(clientConnection.getConnectTimeout())
                        .setSocketTimeout(clientConnection.getReadTimeout())
                        .build());

but in the new http 5 RequestConfig.Builder there is no setSocketTimeout method. How to solve this issue?

1

There are 1 answers

2
Ivajlo Iliev On

Obviously with the new API the idea was to separate different kinds of configurations, because the connection timeout and the socket timeout are connection specific, and the connection request timeout is request specific. That is why I put the socket timeout and the connection timeout in a ConnectionConfig object and add it to the connectionManager.

    ConnectionConfig connectionConfig = ConnectionConfig.custom()
            .setSocketTimeout(Timeout.ofMilliseconds(clientConnection.getReadTimeout()))
            .setConnectTimeout(Timeout.ofMilliseconds(clientConnection.getConnectTimeout()))
            .build();
    connectionManager.setDefaultConnectionConfig(connectionConfig);