HttpClient

时间:2017-02-26 13:47:55

标签: httpclient apache-httpclient-4.x connection-close

我在Web应用程序中面临HttpClient(版本4.5.2)的问题,我的意思是,以多线程方式。在正常情况下,当到达连接请求时,从池中租用连接,然后使用并最终再次释放回池中以再次用于将来的请求,因为以下部分日志用于与id 673890连接。

15 Feb 2017 018:25:54:115 p-1-thread-121 DEBUG PoolingHttpClientConnectionManager:249 - Connection request: [route: {}->http://127.0.0.1:8080][total kept alive: 51; route allocated: 4 of 100; total allocated: 92 of 500]
15 Feb 2017 018:25:54:116 p-1-thread-121 DEBUG PoolingHttpClientConnectionManager:282 - Connection leased: [id: 673890][route: {}->http://127.0.0.1:8080][total kept alive: 51; route allocated: 4 of 100; total allocated: 92 of 500]
15 Feb 2017 018:25:54:116 p-1-thread-121 DEBUG DefaultManagedHttpClientConnection:90 - http-outgoing-673890: set socket timeout to 9000
15 Feb 2017 018:25:54:120 p-1-thread-121 DEBUG PoolingHttpClientConnectionManager:314 - Connection [id: 673890][route: {}->http://127.0.0.1:8080] can be kept alive for 10.0 seconds
15 Feb 2017 018:25:54:121 p-1-thread-121 DEBUG PoolingHttpClientConnectionManager:320 - Connection released: [id: 673890][route: {}->http://127.0.0.1:8080][total kept alive: 55; route allocated: 4 of 100; total allocated: 92 of 500]

在我上面提到的正常方式中多次使用上述连接(id 673890)之后,我注意到代码中发生了以下情况:

15 Feb 2017 018:25:54:130 p-1-thread-126 DEBUG PoolingHttpClientConnectionManager:249 - Connection request: [route: {}->http://127.0.0.1:8080][total kept alive: 55; route allocated: 4 of 100; total allocated: 92 of 500]
15 Feb 2017 018:25:54:130 p-1-thread-126 DEBUG PoolingHttpClientConnectionManager:282 - Connection leased: [id: 673890][route: {}->http://127.0.0.1:8080][total kept alive: 54; route allocated: 4 of 100; total allocated: 92 of 500]
15 Feb 2017 018:25:54:131 p-1-thread-126 DEBUG DefaultManagedHttpClientConnection:90 - http-outgoing-673890: set socket timeout to 9000
15 Feb 2017 018:25:54:133 p-1-thread-126 DEBUG DefaultManagedHttpClientConnection:81 - http-outgoing-673890: Close connection
15 Feb 2017 018:25:54:133 p-1-thread-126 DEBUG PoolingHttpClientConnectionManager:320 - Connection released: [id: 673890][route: {}->http://127.0.0.1:8080][total kept alive: 55; route allocated: 3 of 100; total allocated: 91 of 500]

日志表示连接被请求,租用,使用,关闭然后释放回池中。所以,我的问题是连接关闭的原因?为什么它在关闭后被释放到游泳池?

我知道服务器可以关闭连接,但这是另一种情况。在这种情况下,连接从池中租用,确定为陈旧,因此建立并使用新连接,但上面显示的日志显示了不同的行为。

我知道在HttpClient中连接关闭的两个原因。首先,由于他们的KeepAliveTime已过期而因空闲而关闭。其次,由服务器关闭,这使得连接在池中过时。关闭连接还有其他原因吗?

1 个答案:

答案 0 :(得分:0)

基于Oleg Kalnichevski在HttpClient邮件列表中的回复以及我所做的考试,结果证明问题是因为“联系:关闭”。另一方面发送的标题。可能导致相同情况的另一个原因是使用HTTP / 1.0非持久连接。