11package me .chanjar .weixin .common .util .http ;
22
3- import java .io .IOException ;
4- import java .util .concurrent .TimeUnit ;
5-
3+ import me .chanjar .weixin .common .util .StringUtils ;
64import org .apache .http .annotation .NotThreadSafe ;
75import org .apache .http .auth .AuthScope ;
86import org .apache .http .auth .UsernamePasswordCredentials ;
2321import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
2422import org .apache .http .protocol .HttpContext ;
2523
26- import me .chanjar .weixin .common .util .StringUtils ;
24+ import java .io .IOException ;
25+ import java .util .concurrent .TimeUnit ;
2726
2827/**
2928 * httpclient 连接管理器
3029 */
3130@ NotThreadSafe
32- public class DefaultApacheHttpHttpClientBuilder implements ApacheHttpClientBuilder {
31+ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
3332 private int connectionRequestTimeout = 3000 ;
3433 private int connectionTimeout = 5000 ;
3534 private int soTimeout = 5000 ;
@@ -52,27 +51,20 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte
5251 private String httpProxyUsername ;
5352 private String httpProxyPassword ;
5453
55- /**
56- * 连接管理器
57- */
58- private PoolingHttpClientConnectionManager connectionManager ;
5954 /**
6055 * 闲置连接监控线程
6156 */
6257 private IdleConnectionMonitorThread idleConnectionMonitorThread ;
6358
64- /**
65- * httpClientBuilder
66- */
6759 private HttpClientBuilder httpClientBuilder ;
6860
6961 private boolean prepared = false ;
7062
71- private DefaultApacheHttpHttpClientBuilder () {
63+ private DefaultApacheHttpClientBuilder () {
7264 }
7365
74- public static DefaultApacheHttpHttpClientBuilder get () {
75- return new DefaultApacheHttpHttpClientBuilder ();
66+ public static DefaultApacheHttpClientBuilder get () {
67+ return new DefaultApacheHttpClientBuilder ();
7668 }
7769
7870 @ Override
@@ -111,43 +103,44 @@ public IdleConnectionMonitorThread getIdleConnectionMonitorThread() {
111103
112104 private void prepare () {
113105 Registry <ConnectionSocketFactory > registry = RegistryBuilder .<ConnectionSocketFactory >create ()
114- .register ("http" , this .plainConnectionSocketFactory )
115- .register ("https" , this .sslConnectionSocketFactory )
106+ .register ("http" , this .plainConnectionSocketFactory )
107+ .register ("https" , this .sslConnectionSocketFactory )
116108 .build ();
117- this .connectionManager = new PoolingHttpClientConnectionManager (registry );
118- this .connectionManager .setMaxTotal (this .maxTotalConn );
119- this .connectionManager .setDefaultMaxPerRoute (this .maxConnPerHost );
120- this .connectionManager .setDefaultSocketConfig (
109+
110+ PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager (registry );
111+ connectionManager .setMaxTotal (this .maxTotalConn );
112+ connectionManager .setDefaultMaxPerRoute (this .maxConnPerHost );
113+ connectionManager .setDefaultSocketConfig (
121114 SocketConfig .copy (SocketConfig .DEFAULT )
122- .setSoTimeout (this .soTimeout )
115+ .setSoTimeout (this .soTimeout )
123116 .build ()
124117 );
125118
126119 this .idleConnectionMonitorThread = new IdleConnectionMonitorThread (
127- this . connectionManager , this .idleConnTimeout , this .checkWaitTime );
120+ connectionManager , this .idleConnTimeout , this .checkWaitTime );
128121 this .idleConnectionMonitorThread .setDaemon (true );
129122 this .idleConnectionMonitorThread .start ();
130123
131124 this .httpClientBuilder = HttpClients .custom ()
132- .setConnectionManager (this . connectionManager )
125+ .setConnectionManager (connectionManager )
133126 .setDefaultRequestConfig (
134127 RequestConfig .custom ()
135- .setSocketTimeout (this .soTimeout )
136- .setConnectTimeout (this .connectionTimeout )
137- .setConnectionRequestTimeout (this .connectionRequestTimeout )
128+ .setSocketTimeout (this .soTimeout )
129+ .setConnectTimeout (this .connectionTimeout )
130+ .setConnectionRequestTimeout (this .connectionRequestTimeout )
138131 .build ()
139132 )
140- .setRetryHandler (this .httpRequestRetryHandler );
133+ .setRetryHandler (this .httpRequestRetryHandler );
141134
142135 if (StringUtils .isNotBlank (this .httpProxyHost )
143- && StringUtils .isNotBlank (this .httpProxyUsername )) {
136+ && StringUtils .isNotBlank (this .httpProxyUsername )) {
144137 // 使用代理服务器 需要用户认证的代理服务器
145- CredentialsProvider credsProvider = new BasicCredentialsProvider ();
146- credsProvider .setCredentials (
147- new AuthScope (this .httpProxyHost , this .httpProxyPort ),
148- new UsernamePasswordCredentials (this .httpProxyUsername ,
149- this .httpProxyPassword ));
150- this .httpClientBuilder .setDefaultCredentialsProvider (credsProvider );
138+ CredentialsProvider provider = new BasicCredentialsProvider ();
139+ provider .setCredentials (
140+ new AuthScope (this .httpProxyHost , this .httpProxyPort ),
141+ new UsernamePasswordCredentials (this .httpProxyUsername ,
142+ this .httpProxyPassword ));
143+ this .httpClientBuilder .setDefaultCredentialsProvider (provider );
151144 }
152145
153146 if (StringUtils .isNotBlank (this .userAgent )) {
@@ -187,7 +180,7 @@ public void run() {
187180 wait (this .checkWaitTime );
188181 this .connMgr .closeExpiredConnections ();
189182 this .connMgr .closeIdleConnections (this .idleConnTimeout ,
190- TimeUnit .MILLISECONDS );
183+ TimeUnit .MILLISECONDS );
191184 }
192185 }
193186 } catch (InterruptedException ignore ) {
0 commit comments