Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 691 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Springs RestTemplate default connection pool

#1
Just wondering if RestTemplate out of the box uses connection pooling or does it simply establish a new connection each time ?
Reply

#2
By default RestTemplate creates new Httpconnection every time and closes the connection once done.

If you need to have a connection pooling under rest template then you may use different implementation of the ClientHttpRequestFactory that pools the connections.

new RestTemplate(new HttpComponentsClientHttpRequestFactory())

Reply

#3
I believe `RestTemplate` doesn’t use a connection pool to send requests, it uses a `SimpleClientHttpRequestFactory` that wraps a standard `JDK`’s `HttpURLConnection` opening and closing the connection.

Indeed you can configure `RestTemplate` to use a pooled implementation such as `HttpComponentsClientHttpRequestFactory` but most-likely you might also need to configure some settings to prevent requests from timing out.

I have blogged about this issue at [Troubleshooting Spring's RestTemplate Requests Timeout](

[To see links please register here]

)

Reply

#4
We can use okhttpclient underneath spring's rest template to use connection pooling. A detailed blog on this below

[To see links please register here]

Reply

#5
You can create a Bean for `RestTemplate` and config there :

@Bean
public RestTemplate restTemplate() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(100);
connectionManager.setDefaultMaxPerRoute(20);

RequestConfig requestConfig = RequestConfig
.custom()
.setConnectionRequestTimeout(5000) // timeout to get connection from pool
.setSocketTimeout(5000) // standard connection timeout
.setConnectTimeout(5000) // standard connection timeout
.build();

HttpClient httpClient = HttpClientBuilder.create()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(requestConfig).build();

ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);


return new RestTemplate(requestFactory);
}

And there are a lot config you can do. Refer to

[To see links please register here]


**EDIT**

If you want to use micrometer metrics you should also use a [RestTemplateBuilder][1] for constructing the RestTemplate.


[1]:

[To see links please register here]

Reply

#6
Yes, Spring `RestTemplateBuilder` uses Apache HttpClient for pooling ([usage][1]).
`RestTemplateBuilder` creates `HttpComponentsClientHttpRequestFactory` and uses `HttpClientBuilder`.

`HttpClientBuilder`, by default, sets pool size per route (host) to 5 and total pool size to 10 ([source][2]):

s = System.getProperty("http.maxConnections", "5");
int max = Integer.parseInt(s);
poolingmgr.setDefaultMaxPerRoute(max);
poolingmgr.setMaxTotal(2 * max);


To check connection pool logging set logging level as follows:

org.apache.http.impl.conn.PoolingHttpClientConnectionManager=TRACE

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#7
### In case of using Spring Boot configured with Apache HttpClient (having `org.apache.httpcomponents:httpclient` library in dependencies)


There is a default connection pool configured by PoolingHttpClientConnectionManager

Default concurrent settings for connections (you can find more about default settings here

[To see links please register here]

):

> PoolingHttpClientConnectionManager maintains a maximum limit of connections on a per route basis and in total. Per default this implementation will create no more than 2 concurrent connections per given route and no more 20 connections in total. For many real-world applications these limits may prove too constraining, especially if they use HTTP as a transport protocol for their services.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through