0

I am going through a code that configures dedicated restTemplate for a rest operation. I see the following properties

httpProperties.connection-request-timeout=6100
httpProperties.connect-timeout=6100
httpProperties.read-timeout=6100

My Config class looks like below

@Bean
@ConfigurationProperties(prefix = "httpProperties")
public HttpComponentsClientHttpRequestFactory webSystemHttpRequestFactory() {

    SSLContext sslContext;
    try {

        SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
        HttpClient httpClient = HttpClients.custom().setMaxConnTotal(maxTotalConnection)
                .setMaxConnPerRoute(maxConnectionPerRoute).setSSLSocketFactory(socketFactory).build();
        return  new HttpComponentsClientHttpRequestFactory(httpClient);

    } 
    catch(Exception e) {
    }

    return  new HttpComponentsClientHttpRequestFactory();
}

@Bean(name = "webSystemRestTemplate")
public RestTemplate webSystemRestTemplate() {
    RestTemplate restTemplate = new RestTemplate(webSystemHttpRequestFactory());
    return restTemplate;

}

I can see the logs

o.a.h.i.c.DefaultManagedHttpClientConnection.setSocketTimeout - http-outgoing-1: set socket timeout to 6100

Here is what i want to understand:

  1. How is this value set and to which property by the @CnfigurationProperties annotation?

  2. Is it applicable at the spring boot application level or at each request level?

Please help me understand the concept underlying.

Note: Apache http client version used is 4.5.2

mack
  • 345
  • 5
  • 18
  • possible duplicate https://stackoverflow.com/questions/43232021/using-configurationproperties-annotation-on-bean-method – Toerktumlare Dec 29 '19 at 01:11
  • @ThomasAndolf - I am not able to relate which bean has the property connection-request-timeout, connect-timeout, read-timeout so that the ConfigurationProperties anotation is mapping it to that value. As per my understanding, the properties name should match exactly to the bean attributes. What am i missing here? – mack Dec 29 '19 at 04:40

2 Answers2

0

In the source code for HttpComponentsClientHttpRequestFactory.class there is an object called RequestConfig.class.

In it's source code you can see that there are three parameters.

private final Timeout connectionRequestTimeout;
private final Timeout connectTimeout;
private final Timeout responseTimeout;

These are the ones that the parameters map to using

@ConfigurationProperties(prefix = "httpProperties")

That is not the most common way to set these parameters. But there are multiple ways to set these as pointed out here.

RestTemplate timeout examples

Toerktumlare
  • 12,548
  • 3
  • 35
  • 54
0

The properties are setting the attributes connectionRequestTimeOut, connectTimeOut and readTimeOut of the HttpComponentsClientHttpRequestFactory class. The mapping is done using the ConfigurationProperties annotation that maps the kebab case property names to the bean attributes.

HttpComponentsClientHttpRequestFactory documentation :

HttpComponentsClientHttpRequestFactory

mack
  • 345
  • 5
  • 18