RestTemplate Timeout is not working over 30 seconds

Viewed 717
 @Controller
public class RestTemplateConfig {

  @Bean
  public RestTemplate restTemplate() {
      return new RestTemplate(clientHttpRequestFactory());
  }

  private ClientHttpRequestFactory clientHttpRequestFactory() {
      HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
      factory.setReadTimeout(120000);
      factory.setConnectTimeout(120000);
    return factory;
  }
}

When I make calls to my service to get a large amount of data it takes about 80 -90 seconds to return the response (not a good design, but that is a different problem). I am trying to increase my rest template timeout over 120 seconds on the client-side. The client is a spring batch-job. But it is not working. It is keep timing out around 30 seconds. This is happening when the client is on my local machine and the service that I am making the call is running on a server. And also when the client and service are on a server. Error I am getting 504 gateway exception timeout. The service I am calling working fine, it just takes too much time to return data. But when the client and service running on my local machine, timeout is working fine. Does anyone know what is causing this?

Above is the relevant part of the code.

Here are my findings after trying to debug it

  1. I think the code is not the problem here because it is working locally (may need to add more code to make it work outside the local machine?).
  2. It also works when I try to reduce the timeout like 5 seconds. But will not work the 120-second timeout --> for both local machine and on servers In conclusion: anything below 30 - 36 seconds timeout can be controlled by restTemplate timeout. When it goes above that not working. I know people have actually implemented timeouts above 60 seconds. So it should be possible. So what am I missing here? Some server timeout restrictions? If that is the case, is there a way to override it by making change in my code? I cannot make changes to the servers.

I am pretty sure this is gonna be a good brain teaser for all you guys.

0 Answers
Related