Apache HttpClient: How to auto close connections by server's keep-alive time?

前端 未结 2 1525
没有蜡笔的小新
没有蜡笔的小新 2021-02-09 16:40

Apache HttpClient 4.3b2, HttpCore 4.3.

I use PoolingHttpClientConnectionManager to manage 5 connections concurrently:

PoolingHttpClientConne         


        
相关标签:
2条回答
  • 2021-02-09 17:09

    In PoolingHttpClientConnectionManager class there is a method setValidateAfterInactivity that sets period of connection inactivity in milliseconds. If this period has been exceeded connection pool revalidates connection before passing it to HttpClient. This method is available since v.4.4. In prior versions RequestConfig.Builder.setStaleConnectionCheckEnabled method could have been used.

    0 讨论(0)
  • 2021-02-09 17:18

    This problem is explained in details in HttpClient tutorial

    One of the major shortcomings of the classic blocking I/O model is that the network socket can react to I/O events only when blocked in an I/O operation. When a connection is released back to the manager, it can be kept alive however it is unable to monitor the status of the socket and react to any I/O events. If the connection gets closed on the server side, the client side connection is unable to detect the change in the connection state (and react appropriately by closing the socket on its end).

    If you want expired connections to get pro-actively evicted from the connection pool there is no way around running an additional thread enforcing a connection eviction policy that suits your application.

    0 讨论(0)
提交回复
热议问题