Friday, February 5, 2016

Does Spring's RestTemplate re-use connections?

This question has come up recently in several conversations with my colleagues: does Spring's RestTemplate re-use connections when making REST requests to the same host?

The answer was not that obvious. Even though we are using RestTemplate for executing REST calls, the actual connections are opened by underlying ClientHttpRequestFactory. By default, RestTemplate is created with SimpleClientHttpRequestFactory, which uses standard Java Library's UrlHttpConnection.

Starting from Java ?, UrlHttpConnection supports HTTP 1.1 and keep-alive, if destination server supports it, if certain conditions are satisfied. You can turn it off or specify the maximum number of open connections via system properties, but those are all the options you have.  For several reasons, there is no option to specify how long  the connection can stay open.

However, we have an option to instantiate RestTemplate with HttpComponentsClientHttpRequestFactory, which uses Apache's HttpComponents library to manage connections. This allows greater control over connection pool settings. 

So, the answer to the question in the title: YES, RestTemplate re-uses connections if certain conditions are met.




1 comment:

  1. Wynn Las Vegas - JTG Hub
    Wynn Las Vegas. 1. 서귀포 출장마사지 Casino. 1. 1. 구리 출장마사지 Casino. 2. 1. Wynn 광주광역 출장안마 Las Vegas. 3. 1. Casino. 4. 1. 수원 출장마사지 Casino. 5. 1. Casino. 6. 1. Casino. 7. Casino. 8. Casino. 9. Casino. 10. Casino. 11. 의왕 출장안마 Casino.

    ReplyDelete