4

在使用 Spring 的 RestTemplate 与远程服务器通信时,我遇到了一个问题,即我的应用程序的线程已锁定/阻塞。

这是一个阻塞线程的 jstack 信息:

"pool-1-thread-8" prio=10 tid=0x00007fbfd012d800 nid=0x27b1 runnable [0x00007fbfd7dfb000]
   java.lang.Thread.State: RUNNABLE
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.read(SocketInputStream.java:129)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
        at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
        - locked <0x00007fbfe40fe1f8> (a java.io.BufferedInputStream)
        at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:687)
        at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:632)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1072)
        - locked <0x00007fbfe40fe2f0> (a sun.net.www.protocol.http.HttpURLConnection)
        at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:373)
        at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:47)
        at org.springframework.http.client.AbstractClientHttpResponse.getStatusCode(AbstractClientHttpResponse.java:32)
        at org.springframework.web.client.DefaultResponseErrorHandler.getHttpStatusCode(DefaultResponseErrorHandler.java:55)
        at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:49)
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:489)
        at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:452)
        at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:302)
        at com.xxx.activation.service.action.impl.RestServiceImpl.sendContactRequest(RestServiceImpl.java:37)
        at com.xxx.activation.service.action.impl.SendContactRequestAction.perform(SendContactRequestAction.java:125)
        at com.xxx.activation.service.action.impl.DefaultStateActionService.doAction(DefaultStateActionService.java:75)
        at com.xxx.activation.service.action.impl.ActionProcess$StateActionTask.call(ActionProcess.java:54)
        at com.xxx.activation.service.action.impl.ActionProcess$StateActionTask.call(ActionProcess.java:41)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:619)


我该如何解决这个问题?
这与 java.net.URLConnection 的 readTimeout 有关吗?使用Sring的RestTemplate时,底层socket的connectTimeoutreadTimeout
的默认值是多少?

提前致谢。

4

2 回答 2

1

如果您使用的是默认的 Rest Template,请将 read Timeout 添加到默认请求工厂,当前默认为 SimpleClientHttpRequestFactory

final SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    requestFactory.setReadTimeout(10_000);  // 10 sec as needed by us
    final RestTemplate restTemplate = new RestTemplate(requestFactory);

如果不是,则检查 http 连接 readTimeout 不应为 0 并为其分配一个值。

于 2019-12-27T19:08:01.733 回答
0

此处的帖子描述了您的问题

尝试为您的 REST 调用设置一个超时时间,并确保 REST 服务已启动并正在运行。检查您的 REST 服务是否确实在尝试获取大量信息。

于 2014-06-27T05:19:56.493 回答