Search code examples
javaspringresttemplate

Java Spring RestTemplate timeout does not work properly


I have such scenario where I do a request to an API , let's say I am doing that request using restTemplate.exchange method. I do set the timeouts like this:

((HttpComponentsClientHttpRequestFactory) requestFactory).setConnectTimeout(200090);
      ((HttpComponentsClientHttpRequestFactory) requestFactory).setReadTimeout(99999999);
      ((HttpComponentsClientHttpRequestFactory) requestFactory).setConnectionRequestTimeout(200900);

and when I am online everything is OK, however, when I do press to make the request and I eject the Network cable, it waits like 10-20 seconds (not as my timeout) and it throws an SocketTimeoutException and that's it, I debugged , tried to apply many of solutions however could not find anything precious. Have anyone encountered this with RestTemplate and Spring? (In debugging everything is set accordingly, all my timeouts are present, even if I use factory it uses the correct instance) .

My desired scenario is: Enter for request -> Disconnect network cable -> The applications follows the timeout set in it, and only after it it gives error or whatever. How can I fix this?

Java JDK 1.8 , httpclient-4.5.12, Spring 4.3.12-RELEASE.


Solution

  • So, whoever meets this, the problem were in that of hosts ip, if it is present it takes 20 seconds from somewhere, however if it is not available it takes our timeout.