httpClient 连接池管理器 validateAfterInactivity

问题描述 投票:0回答:2

我正在查看PoolingHttpClientConnectionManager的文档https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.html

有一个 API setValidateAfterInactivity。 validateAfterInactivity 对我来说不是很清楚。它说 - “定义不活动时间(以毫秒为单位),在此之后持久连接必须在租给消费者之前重新验证”

它到底如何重新验证连接?想了解一下过程。它是否向服务器发送任何 http 请求或需要重新验证的内容,或者其他内容?

它用于重新验证连接的标准/机制是什么?这一切是如何运作的?

java spring connection httpclient connection-pooling
2个回答
0
投票

org.apache.http.pool.AbstractConnPool

if (validateAfterInactivity > 0)  {
    if (leasedEntry.getUpdated() + validateAfterInactivity <= System.currentTimeMillis()) {
        if (!validate(leasedEntry)) {
            leasedEntry.close();
            release(leasedEntry, false);
            continue;
        }
    }
}

并且在

org.apache.http.impl.conn.CPool
或其他类似的实现中:

@Override
protected boolean validate(final CPoolEntry entry) {
    return !entry.getConnection().isStale();
}

最后,

isStale
org.apache.http.HttpConnection

/**
 * Checks whether this connection has gone down.
 * Network connections may get closed during some time of inactivity
 * for several reasons. The next time a read is attempted on such a
 * connection it will throw an IOException.
 * This method tries to alleviate this inconvenience by trying to
 * find out if a connection is still usable. Implementations may do
 * that by attempting a read with a very small timeout. Thus this
 * method may block for a small amount of time before returning a result.
 * It is therefore an <i>expensive</i> operation.
 *
 * @return  {@code true} if attempts to use this connection are
 *          likely to succeed, or {@code false} if they are likely
 *          to fail and this connection should be closed
 */
boolean isStale();

org.apache.http.HttpConnection
有不同的实现,它们几乎按照文档的说明进行操作,要么检查是否存在文件结束,要么尝试从连接中读取。默认情况下,
validateAfterInactivity
设置为 2 秒。


-2
投票

它使用 JDBC 连接进行验证。

                    final ManagedHttpClientConnection conn = poolEntry.getConnection();
                    if (conn != null) {
                        conn.activate();
                    } else {
                        poolEntry.assignConnection(connFactory.createConnection(null));
                    }
                    if (log.isDebugEnabled()) {
                        log.debug("Connection leased: " + ConnPoolSupport.formatStats(
                                poolEntry.getConnection(), route, state, pool));
                    }

源代码这里

© www.soinside.com 2019 - 2024. All rights reserved.