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

后端 未结 1 988
醉话见心
醉话见心 2021-02-14 08:07

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

1条回答
  •  深忆病人
    2021-02-14 08:54

    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?

    0 讨论(0)
提交回复
热议问题