this code used to work but now got setReadTimeout method marked as deprecated error when update to spring 3
@Configuration
class RestTemplateConfig(
private val corePoolSize: Int = 100
) {
private fun pooledConnectionManager() = PoolingHttpClientConnectionManager()
.apply {
maxTotal = corePoolSize
defaultMaxPerRoute = corePoolSize
}
private fun pooledHttpClient() = HttpClientBuilder.create()
.setConnectionManager(pooledConnectionManager())
.build()
@Bean
@Primary
fun restTemplate(builder: RestTemplateBuilder): RestTemplate {
return builder.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.requestFactory { ->
HttpComponentsClientHttpRequestFactory(
pooledHttpClient()
)
}
.setConnectTimeout(Duration.ofSeconds(30))
.setReadTimeout(Duration.ofSeconds(30))
.build()
}
}
Comment From: mdeinum
The javadoc of HttpComponentsClientHttpRequestFactory explains what the replacement is.
This feels more of a question for stackoverflow on what to replace it with.
Comment From: wilkinsona
The underlying method in Framework has been deprecated and no longer does anything. It's javadoc describes what to do instead:
/**
* As of version 6.0, setting this property has no effect.
* <p>To change the socket read timeout, use {@link SocketConfig.Builder#setSoTimeout(Timeout)},
* supply the resulting {@link SocketConfig} to
* {@link org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder#setDefaultSocketConfig(SocketConfig)},
* use the resulting connection manager for
* {@link org.apache.hc.client5.http.impl.classic.HttpClientBuilder#setConnectionManager(HttpClientConnectionManager)},
* and supply the built {@link HttpClient} to {@link #HttpComponentsClientHttpRequestFactory(HttpClient)}.
* @deprecated as of 6.0, in favor of {@link SocketConfig.Builder#setSoTimeout(Timeout)}, see above.
*/
@Deprecated(since = "6.0", forRemoval = true)
public void setReadTimeout(int timeout) {
logger.warn("HttpComponentsClientHttpRequestFactory.setReadTimeout has no effect");
}
Spring Boot can still do this for you but only if you don't want to further customize the underlying HTTP client. As you're customizing the PoolingHttpClientConnectionManager you will have to also configure the read timeout yourself.