0

I have spring boot infrastructure with few microservices. They communicating pretty well between each other. One of the microservices have some third party calls, calling some third party services through http. I have defined my own RestTemplate and in the init() method I am setting connectTimeout and readTimeout. From the stack traces I see that my rest template is used.

  @PostConstruct
  public void init() {

    HttpComponentsClientHttpRequestFactory requestFactory =
        new HttpComponentsClientHttpRequestFactory();
    requestFactory.setConnectTimeout(connectTimeout);
    requestFactory.setReadTimeout(readTimeout);
    this.restTemplate = new RestTemplate(requestFactory);
  }

The problem is that some of the requests are handing for few minutes and no exceptions are thrown. (my timeouts are much less - about 5-10 seconds) What may be the cause? Any ideas?

saferJo
  • 497
  • 1
  • 5
  • 16

1 Answers1

1

Missing the call to setConnectionRequestTimeout(...);

HttpComponentsClientHttpRequestFactory requestFactory =
        new HttpComponentsClientHttpRequestFactory();
requestFactory.setConnectTimeout(connectTimeout);
requestFactory.setReadTimeout(readTimeout);
requestFactory.setConnectionRequestTimeout(...);
this.restTemplate = new RestTemplate(requestFactory);

Spring RestTemplate timeout

Ryan Dake
  • 84
  • 6
  • that was something I was thinking about, but can you clarify a bit more how that will behave? when we will have connect timeout and connectRequest timeout? thanks in advance. – saferJo Apr 22 '19 at 13:13
  • @saferJo Hey, you can find clarification for the difference (https://stackoverflow.com/questions/39256157/apache-http-setsockettimout-vs-setconnecttimout-vs-setconnectionrequesttimeout/44222752) . Looking further into what you are trying to do I believe what you really want to set is the socketTimeout see (https://www.baeldung.com/rest-template) section 8. – Ryan Dake Apr 22 '19 at 14:03