Default keep-alive time for a HttpConnection when using Spring Rest Template

拟墨画扇 提交于 2019-12-05 00:49:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!