httpclient的disableConnectionState方法工作流程
序
本文主要研究一下httpclient的disableConnectionState
disableConnectionState
org/apache/http/impl/client/HttpClientBuilder.java
/** * Disables connection state tracking. */ public final HttpClientBuilder disableConnectionState() { connectionStateDisabled = true; return this; } public CloseableHttpClient build() { //...... UserTokenHandler userTokenHandlerCopy = this.userTokenHandler; if (userTokenHandlerCopy == null) { if (!connectionStateDisabled) { userTokenHandlerCopy = DefaultUserTokenHandler.INSTANCE; } else { userTokenHandlerCopy = NoopUserTokenHandler.INSTANCE; } } ClientExecChain execChain = createMainExec( requestExecCopy, connManagerCopy, reuseStrategyCopy, keepAliveStrategyCopy, new ImmutableHttpProcessor(new RequestTargetHost(), new RequestUserAgent(userAgentCopy)), targetAuthStrategyCopy, proxyAuthStrategyCopy, userTokenHandlerCopy); //...... } }
HttpClientBuilder提供了disableConnectionState方法可以設置connectionStateDisabled為true,在該值為true時userTokenHandlerCopy為NoopUserTokenHandler.INSTANCE,而userTokenHandlerCopy是作為創(chuàng)建ClientExecChain(MainClientExec)的參數(shù)之一
execute
org/apache/http/impl/execchain/MainClientExec.java
Object userToken = context.getUserToken(); if (userToken == null) { userToken = userTokenHandler.getUserToken(context); context.setAttribute(HttpClientContext.USER_TOKEN, userToken); } if (userToken != null) { connHolder.setState(userToken); }
MainClientExec的execute方法會通過context.getUserToken()獲取userToken,在userToken為null時會通過serTokenHandler.getUserToken(context)獲取userToken然后設置到context中,最后將userToken設置到connHolder的state
UserTokenHandler
org/apache/http/client/UserTokenHandler.java
public interface UserTokenHandler { /** * The token object returned by this method is expected to uniquely * identify the current user if the context is user specific or to be * {@code null} if it is not. * * @param context the execution context * * @return user token that uniquely identifies the user or * {@code null} if the context is not user specific. */ Object getUserToken(HttpContext context); }
UserTokenHandler定義了getUserToken方法
DefaultUserTokenHandler
org/apache/http/impl/client/DefaultUserTokenHandler.java
@Contract(threading = ThreadingBehavior.IMMUTABLE) public class DefaultUserTokenHandler implements UserTokenHandler { public static final DefaultUserTokenHandler INSTANCE = new DefaultUserTokenHandler(); @Override public Object getUserToken(final HttpContext context) { final HttpClientContext clientContext = HttpClientContext.adapt(context); Principal userPrincipal = null; final AuthState targetAuthState = clientContext.getTargetAuthState(); if (targetAuthState != null) { userPrincipal = getAuthPrincipal(targetAuthState); if (userPrincipal == null) { final AuthState proxyAuthState = clientContext.getProxyAuthState(); userPrincipal = getAuthPrincipal(proxyAuthState); } } if (userPrincipal == null) { final HttpConnection conn = clientContext.getConnection(); if (conn.isOpen() && conn instanceof ManagedHttpClientConnection) { final SSLSession sslsession = ((ManagedHttpClientConnection) conn).getSSLSession(); if (sslsession != null) { userPrincipal = sslsession.getLocalPrincipal(); } } } return userPrincipal; } private static Principal getAuthPrincipal(final AuthState authState) { final AuthScheme scheme = authState.getAuthScheme(); if (scheme != null && scheme.isComplete() && scheme.isConnectionBased()) { final Credentials creds = authState.getCredentials(); if (creds != null) { return creds.getUserPrincipal(); } } return null; } }
DefaultUserTokenHandler從context中獲取userPrincipal
NoopUserTokenHandler
org/apache/http/impl/client/NoopUserTokenHandler.java
@Contract(threading = ThreadingBehavior.IMMUTABLE) public class NoopUserTokenHandler implements UserTokenHandler { public static final NoopUserTokenHandler INSTANCE = new NoopUserTokenHandler(); @Override public Object getUserToken(final HttpContext context) { return null; } }
NoopUserTokenHandler的getUserToken則返回null
小結
httpclient的disableConnectionState設置了ClientExecChain(MainClientExec
)的UserTokenHandler為NoopUserTokenHandler,而MainClientExec的execute方法會通過context.getUserToken()獲取userToken,在userToken為null時會通過serTokenHandler.getUserToken(context)獲取userToken然后設置到context中,最后將userToken設置到connHolder的state。
connHolder的state與userToken掛鉤起來歧義挺大的
以上就是httpclient的disableConnectionState的詳細內容,更多關于httpclient disableConnectionState的資料請關注腳本之家其它相關文章!
- httpclient connect連接請求方法源碼解讀
- httpclient getPoolEntryBlocking連接池方法源碼解讀
- httpclient staleConnectionCheckEnabled獲取連接流程解析
- 解讀httpclient的validateAfterInactivity連接池狀態(tài)檢測
- 探索HttpClient中的close方法及其對連接的影響
- HttpClient的RedirectStrategy重定向處理核心機制
- HttpClient HttpRoutePlanner接口確定請求目標路由
- httpclient ConnectionHolder連接池連接保持源碼解析
相關文章
springboot讀取application.yaml文件數(shù)據(jù)的方法
這篇文章主要為大家詳細介紹了springboot讀取application.yaml文件數(shù)據(jù)的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07MyBatis批量插入數(shù)據(jù)到Oracle數(shù)據(jù)庫中的兩種方式(實例代碼)
本文通過實例代碼給大家分享了MyBatis批量插入數(shù)據(jù)到Oracle數(shù)據(jù)庫中的兩種方式,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-09-09Hikari連接池使用SpringBoot配置JMX監(jiān)控實現(xiàn)
Hikari是Spring Boot默認的數(shù)據(jù)庫連接池。區(qū)別于C3P0直接通過連接池對象獲取各項狀態(tài)指標,Hikari需要通過JMX來獲取。本文就詳細的來介紹一下,感興趣的可以了解一下2021-07-07