0

I have an application that uses Spring RestTemplate. Some times, it gets stuck (last time, it was stuck for 2 days before we noticed and restarted it):

"scheduling-1" #19 prio=5 os_prio=0 tid=0x00007fbcfdfc0800 nid=0x36 runnable [0x00007fbc901c7000]
   java.lang.Thread.State: RUNNABLE
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:171)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
    - locked <0x00000000c4ae36d0> (a java.io.BufferedInputStream)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1593)
    - locked <0x00000000c4aecc88> (a sun.net.www.protocol.http.HttpURLConnection)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
    - locked <0x00000000c4aecc88> (a sun.net.www.protocol.http.HttpURLConnection)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
    at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:55)
    at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:55)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:765)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:735)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:578)

I build the RestTemplate using restTemplateBuilder.build().

I would assume that RestTemplate have some kind of default timeout configuration to drop the connection after some time, but it is not dropping it, so I figure the connection is in some weird state. Can I somehow configure the RestTemplate to drop the connection after some amount of time, even if it believes the connection to be fine?

Saita
  • 964
  • 11
  • 36
  • I am experiencing something a little different. Sometimes, when I use that same RestTemplate instance to fetch information from somewhere else and it takes too long, I get a ResourceAccessException with a message like "read timeout", so it's just this other particular weird situation where it hangs for days without dropping the connection. – Saita Oct 29 '19 at 13:37
  • You can set readTimeout for your request as given in shared thread. – www.hybriscx.com Oct 29 '19 at 13:40
  • 1
    set both the read and connection timeout, the defaults are to wait indefinitely (-1) if you use the default `ClientRequestFactory` for others it depends on the library being used. You can do this by setting them to some reasonable values on the `RestTemplateBuilder` with `setReadTimeout` and `setConnectionTimeout`. – M. Deinum Oct 29 '19 at 15:22

0 Answers0