简体   繁体   中英

How PoolingHttpClientConnectionManager manage connections?

I have a question and it is What is httpconnection of PoolingHttpClientConnectionManager?.

I know that If we use PoolingHttpClientConnectionManager , it reduces the time for spending connection establishment (such as ssl handshake , tcp enter code herehandshake etc) because it reuses the connections.

However, what I understand about http connection reusing is keep-alive and we can use it when server support it. If host doesn't support keep-alive connection, we cannot communicate with the host with keep-alive.

So, here are my questions,

If I use PoolingHttpClientConnectionManager to manage connections on non keep-alive server environment, does Connectionmanager manage connections? or it creates connection per request?

If ConnectionManager manages connections, how ConnectionManager keep connection? does the manager send bytes periodically?

If you don't define HttpClient will act as connection can be kept alive indefinitely, from Apache http docs :

If the Keep-Alive header is not present in the response, HttpClient assumes the connection can be kept alive indefinitely.

If you want to define Keep-Alive Strategy see example :

ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
    @Override
    public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
        HeaderElementIterator it = new BasicHeaderElementIterator
            (response.headerIterator(HTTP.CONN_KEEP_ALIVE));
        while (it.hasNext()) {
            HeaderElement he = it.nextElement();
            String param = he.getName();
            String value = he.getValue();
            if (value != null && param.equalsIgnoreCase
               ("timeout")) {
                return Long.parseLong(value) * 1000;
            }
        }
        return 5 * 1000;
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM