java 使用 Spring Rest 模板时 HttpConnection 的默认保持活动时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35185025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Default keep-alive time for a HttpConnection when using Spring Rest Template
提问by sam
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.
我想知道在通过 Spring rest 模板创建新连接之前 HttpConnection 在不活动时保持活动状态的时间。我查看了默认的 Connection Time-Out 和 Read Time-Out 参数,但我相信这些是在由于某些故障等原因未建立连接时在连接超时的情况下使用的。
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.
我正在寻找的是,如果没有活动(或)不活动,连接保持活动状态的时间,以及如何通过 Spring Rest 模板(或)底层机制进行配置。
采纳答案by jny
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:
默认情况下,RestTemplate
使用SimpleClientHttpRequestFactory
这反过来又打开Java的HttpURLConnection
它默认支持保持活跃在一定条件下。如果您想更多地控制连接的处理方式,您可以HttpComponentsClientHttpRequestFactory
使用 ApacheHttpClient
库创建 restTemplate ,例如:
@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?