Fix Request: HttpClientBuilderWithoutTimeouts, not valid for HttpClient 5
Closed this issue · 1 comments
jborgers commented
The API of HttpClient 5 is different from HttpClient 4, for this rule relevant:
- ConnectTimeout has been moved from RequestConfig to ConnectionConfig
- ConnectionConfig also has property socketTimeout (=readTimeout)
- RequestConfig readTimeout is renamed to responseTimeout
- Spring HttpComponentsClientHttpRequestFactory setReadTimeout is removed
Example:
class ExampleTimeouts {
@Bean
public RestTemplate restTemplate() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(httpClient());
// alternative way to set the 3 timeouts:
//factory.setConnectionRequestTimeout(connectTimeoutInMillis); // 1
//factory.setConnectTimeout(connectTimeoutInMillis); // 2
return new RestTemplate(factory);
}
private CloseableHttpClient httpClient() {
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(connectTimeoutInMillis, TimeUnit.MILLISECONDS) // 1
.setResponseTimeout(readTimeoutInMillis, TimeUnit.MILLISECONDS) // 3
.build();
final ConnectionConfig connConfig = ConnectionConfig.custom()
.setConnectTimeout(connectTimeoutInMillis, TimeUnit.MILLISECONDS) // 2
.setSocketTimeout(readTimeoutInMillis, TimeUnit.MILLISECONDS).build(); // 3
final PoolingHttpClientConnectionManager connectionManager =
new BasicHttpClientConnectionManager(socketFactoryRegistry);
connectionManager.setConnectionConfig(connConfig);
return HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setConnectionManager(connectionManager)
.disableConnectionState()
.build();
}
}
jborgers commented
Fix: The rule now checks if an import of Apache HttpClient 4 is used.
We need a new rule for version 5