我在使用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时,底层套接字的 connectTimeout 和 readTimeout 的默认值是什么?
提前致谢。
答案 0 :(得分:0)
帖子here描述了您的问题
尝试为REST调用设置超时,并确保REST服务已启动并正在运行。检查您的REST服务是否确实尝试获取大量信息。
答案 1 :(得分:0)
如果使用默认的Rest模板,则将读取超时添加到默认请求Factory(当前默认为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并为其分配一个值。