I am trying to know how long a HttpConnection is kept alive when inactive, before a new connection is created via Spring rest Template. I looked at default Connection Time-Out
By default RestTemplate
uses SimpleClientHttpRequestFactory
which in turn opens Java's HttpURLConnection which by default supports keep-alive under certain conditions. If you want more control over how connections are handled, you can create restTemplate with HttpComponentsClientHttpRequestFactory, which uses Apache HttpClient
library, e.g:
@Bean
RestTemplate restTemplate(SimpleClientHttpRequestFactory factory) {
return new RestTemplate(factory);
}
You can also see some discussions here:
How to Reuse HttpUrlConnection?
Persistent HttpURLConnection in Java
How to use RestTemplate efficiently in Multithreaded environment?