在 Apache 4.x 客户端中控制请求超时
Controlling Request Timeout in Apache 4.x Client
如何将 HttpClientConnectionManager
的超时设置为超过 2 分钟?
我们正在连接到一个很长的 运行 SOAP 服务,该服务会在返回 HTTP 响应之前处理几分钟 (5-8)。我们无法控制此服务,因此在收到响应之前需要等待很长时间 activity。
使用 Apache HttpClient 4.3.2 库,我们注意到连接管理器似乎正在关闭连接,原因是 activity 2 分钟后:
16:14:45.805 DEBUG o.a.h.headers.onRequestSubmitted(124) - http-outgoing-0 >> POST /someendpoint.do?SOAP HTTP/1.1
16:14:45.805 DEBUG o.a.h.headers.onRequestSubmitted(127) - http-outgoing-0 >> content-type: application/soap+xml
16:14:45.805 DEBUG o.a.h.headers.onRequestSubmitted(127) - http-outgoing-0 >> Content-Length: 536
16:14:45.806 DEBUG o.a.h.headers.onRequestSubmitted(127) - http-outgoing-0 >> Host: someserver.com
16:14:45.806 DEBUG o.a.h.headers.onRequestSubmitted(127) - http-outgoing-0 >> Connection: Keep-Alive
16:14:45.806 DEBUG o.a.h.headers.onRequestSubmitted(127) - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.3.2 (java 1.5)
16:14:45.806 DEBUG o.a.h.headers.onRequestSubmitted(127) - http-outgoing-0 >> Accept-Encoding: gzip,deflate
16:14:45.806 DEBUG o.a.h.headers.onRequestSubmitted(127) - http-outgoing-0 >> Authorization: Basic Y290GARBAGE36Om9uZXR
16:16:46.750 DEBUG o.a.h.i.c.DefaultManagedHttpClientConnection.close(79) - http-outgoing-0: Close connection
16:16:46.750 DEBUG o.a.h.i.c.DefaultManagedHttpClientConnection.shutdown(87) - http-outgoing-0: Shutdown connection
16:16:46.750 DEBUG o.a.h.i.e.MainClientExec.abortConnection(126) - Connection discarded
16:16:46.751 DEBUG o.a.h.i.c.DefaultManagedHttpClientConnection.close(79) - http-outgoing-0: Close connection
16:16:46.751 DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager.releaseConnection(282) - Connection released: [id: 0][route: {tls}->http://http-proxy.mydomain.com:8080->https://someserver.com:443][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
16:16:46.753 INFO o.a.h.i.e.RetryExec.execute(93) - I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond
16:16:46.758 DEBUG o.a.h.i.e.RetryExec.execute(98) - The target server failed to respond
org.apache.http.NoHttpResponseException: The target server failed to respond
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:143) ~[httpclient-4.3.2.jar:4.3.2]
我们已尝试 connMgr.closeIdleConnections( 10, TimeUnit.MINUTES );
,指定我们自己的 ConnectionKeepAliveStrategy
,并在请求配置中设置超时:
config = RequestConfig.copy(RequestConfig.DEFAULT)
.setSocketTimeout(soTimeout)
.setConnectTimeout(connTimeout)
.setConnectionRequestTimeout(rqTimeout)
.build();
我们是否缺少一些补充设置,例如设置超时 here 并设置标志 there 或两者 this 和 that 设置为相同的值?
当预计服务器超过 2 分钟无响应时,防止连接关闭的正确方法是什么?
NoHttpResponseException 基本上意味着连接被对端端点(或中介)终止。您在客户端的超时设置看起来非常好。
如何将 HttpClientConnectionManager
的超时设置为超过 2 分钟?
我们正在连接到一个很长的 运行 SOAP 服务,该服务会在返回 HTTP 响应之前处理几分钟 (5-8)。我们无法控制此服务,因此在收到响应之前需要等待很长时间 activity。
使用 Apache HttpClient 4.3.2 库,我们注意到连接管理器似乎正在关闭连接,原因是 activity 2 分钟后:
16:14:45.805 DEBUG o.a.h.headers.onRequestSubmitted(124) - http-outgoing-0 >> POST /someendpoint.do?SOAP HTTP/1.1
16:14:45.805 DEBUG o.a.h.headers.onRequestSubmitted(127) - http-outgoing-0 >> content-type: application/soap+xml
16:14:45.805 DEBUG o.a.h.headers.onRequestSubmitted(127) - http-outgoing-0 >> Content-Length: 536
16:14:45.806 DEBUG o.a.h.headers.onRequestSubmitted(127) - http-outgoing-0 >> Host: someserver.com
16:14:45.806 DEBUG o.a.h.headers.onRequestSubmitted(127) - http-outgoing-0 >> Connection: Keep-Alive
16:14:45.806 DEBUG o.a.h.headers.onRequestSubmitted(127) - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.3.2 (java 1.5)
16:14:45.806 DEBUG o.a.h.headers.onRequestSubmitted(127) - http-outgoing-0 >> Accept-Encoding: gzip,deflate
16:14:45.806 DEBUG o.a.h.headers.onRequestSubmitted(127) - http-outgoing-0 >> Authorization: Basic Y290GARBAGE36Om9uZXR
16:16:46.750 DEBUG o.a.h.i.c.DefaultManagedHttpClientConnection.close(79) - http-outgoing-0: Close connection
16:16:46.750 DEBUG o.a.h.i.c.DefaultManagedHttpClientConnection.shutdown(87) - http-outgoing-0: Shutdown connection
16:16:46.750 DEBUG o.a.h.i.e.MainClientExec.abortConnection(126) - Connection discarded
16:16:46.751 DEBUG o.a.h.i.c.DefaultManagedHttpClientConnection.close(79) - http-outgoing-0: Close connection
16:16:46.751 DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager.releaseConnection(282) - Connection released: [id: 0][route: {tls}->http://http-proxy.mydomain.com:8080->https://someserver.com:443][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
16:16:46.753 INFO o.a.h.i.e.RetryExec.execute(93) - I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond
16:16:46.758 DEBUG o.a.h.i.e.RetryExec.execute(98) - The target server failed to respond
org.apache.http.NoHttpResponseException: The target server failed to respond
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:143) ~[httpclient-4.3.2.jar:4.3.2]
我们已尝试 connMgr.closeIdleConnections( 10, TimeUnit.MINUTES );
,指定我们自己的 ConnectionKeepAliveStrategy
,并在请求配置中设置超时:
config = RequestConfig.copy(RequestConfig.DEFAULT)
.setSocketTimeout(soTimeout)
.setConnectTimeout(connTimeout)
.setConnectionRequestTimeout(rqTimeout)
.build();
我们是否缺少一些补充设置,例如设置超时 here 并设置标志 there 或两者 this 和 that 设置为相同的值?
当预计服务器超过 2 分钟无响应时,防止连接关闭的正确方法是什么?
NoHttpResponseException 基本上意味着连接被对端端点(或中介)终止。您在客户端的超时设置看起来非常好。