问题
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 and Read Time-Out parameters, but I believe these are used in the context of connection time out when the connection is not established due to some failure etc.
What I am looking for is, how long a connection is kept alive if there is no activity (or) inactive, and how to configure this via Spring Rest Template (or) the underlying mechanism.
回答1:
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?
来源:https://stackoverflow.com/questions/35185025/default-keep-alive-time-for-a-httpconnection-when-using-spring-rest-template