【发布时间】:2015-10-28 21:19:19
【问题描述】:
如何将HttpClientConnectionManager 的超时时间设置为超过 2 分钟?
我们正在连接一个长时间运行的 SOAP 服务,该服务会在返回 HTTP 响应之前处理数 (5-8) 分钟。我们无法控制此服务,因此我们需要在收到响应之前允许长时间的活动。
使用 Apache HttpClient 4.3.2 库,我们注意到由于 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 和 是否设置为相同的值?
当服务器预计超过 2 分钟没有响应时,防止连接关闭的正确方法是什么?
【问题讨论】:
标签: java apache-httpclient-4.x