Search code examples
spring-bootapache-httpclient-4.xresttemplatespring-rest

setting timeout using @ConfigurationProperties in SpringBoot


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


Solution

  • 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