-
Notifications
You must be signed in to change notification settings - Fork 40.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecate+Remove ability to provide a custom ClientHttpRequestFactory instance in RestTemplateBuilder #11255
Comments
Perhaps we could make the |
I think what you're describing here is the expected behavior, since:
In most cases, sharing a One could also argue the following: mutating the request factory options at runtime is supposed to change the request behaviour as well. I don't think Spring Boot can change anything in this model. If we were to replicate the factory detection for that (by cloning/creating request factory instances for each builder call), we'd be breaking the expected behaviour for many: resources wouldn't be shared anymore between |
I agree that the On the other hand, the two
It may be not very obvious to the user of I would at least suppose to add a warning to the javadoc of
However, our concrete problem isn't solved this way ;-). We (lvm-it) like the |
I changed my mind a bit and I think @wilkinsona is right. Look at the following example; the
I agree that providing your own factory instance and changing the connection timeout down the line is a bit strange. But I think that, in general, the |
After discussing that with the team (and given my previous comment), we've found that the inconsistency comes from the Here's what we should do:
After some thoughts, you should indeed share and reuse |
We might have trouble deprecating I'm tempted to leave 1.5 as it is, perhaps with some Javadoc warning that the instance is shared. |
This commit adds a javadoc note about a usability issue described in gh-11255. While `RestTemplateBuilder` is an immutable class, providing a custom instance of request factory and deriving several builders/templates from that point may have some unexpected behavior, since that instance is shared amongst builders instances. This issue is fixed in Spring Boot 2.0 with a replacement method that leverages a `Supplier<ClientHttpRequestFactory>` instead. See gh-11255
Problem
When
ClientHttpRequestFactory
isn't explicitly set,RestTemplateBuilder
invokesdetectRequestFactory()
internally for every new RestTemplate. So every RestTemplate gets it's own 'prototype'ClientHttpRequestFactory
.On the other hand, when
ClientHttpRequestFactory
is explicitly set in RestTemplateBuilder, all subsequent build RestTemplates get the same 'singleton'ClientHttpRequestFactory
.During
ResttemplateBuilder.build()
method, the currentClientHttpRequestFactory
becomes customized with timout values (connection-timeout and read-timeout). This leads to problems when multiple RestTemplates with different timeout values are created.We have a Spring-Boot application that provides a
RestTemplateBuilder
bean which is already provided with aHttpComponentsHttpRequestFactory
. Two service beans uses theRestTemplateBuilder
to create a custom RestTemplate with different connection-timouts:Workaround
Our current Workaround: we do not set
HttpComponentsHttpRequestFactory
explicitly but letRestTemplateBuilder
detect this factory. For customizing (i.e. setting max-connections und max-connections-per-route) we use the systemproperties approach of the httpcomponents httpclient.Possible Solution (breaking the api in spring-web)
As far as I can see, all supported http implementations (httpcommons, netty, okhttp, okhttp3, SimpleClientHttpRequestFactory) allow a per request setting of connection- and read-timeouts. So a solution would be not to inject these timeouts into the
ClientHttpRequestFactory
but hold it as 'RequestSettings' in theRestTemplateBuilder
and give them to every new created RestTemplate.The RestTemplate in turn has to provide these settings as additional method-parameter to
ClientHttpRequestFactory.createRequest()
. This will however break the api of ClientHttpRequestFactory...The text was updated successfully, but these errors were encountered: