11package me .chanjar .weixin .common .util .http ;
22
3- import me .chanjar .weixin .common .util .StringUtils ;
3+ import java .io .IOException ;
4+ import java .util .concurrent .TimeUnit ;
5+
46import org .apache .http .annotation .NotThreadSafe ;
57import org .apache .http .auth .AuthScope ;
68import org .apache .http .auth .UsernamePasswordCredentials ;
2123import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
2224import org .apache .http .protocol .HttpContext ;
2325
24- import java .io .IOException ;
25- import java .util .concurrent .TimeUnit ;
26+ import me .chanjar .weixin .common .util .StringUtils ;
2627
2728/**
2829 * httpclient 连接管理器
@@ -33,7 +34,7 @@ public class DefaultApacheHttpHttpClientBuilder implements ApacheHttpClientBuild
3334 private int connectionTimeout = 5000 ;
3435 private int soTimeout = 5000 ;
3536 private int idleConnTimeout = 60000 ;
36- private int checkWaitTime = 5000 ;
37+ private int checkWaitTime = 60000 ;
3738 private int maxConnPerHost = 10 ;
3839 private int maxTotalConn = 50 ;
3940 private String userAgent ;
@@ -74,86 +75,95 @@ public static DefaultApacheHttpHttpClientBuilder get() {
7475 return new DefaultApacheHttpHttpClientBuilder ();
7576 }
7677
78+ @ Override
7779 public ApacheHttpClientBuilder httpProxyHost (String httpProxyHost ) {
7880 this .httpProxyHost = httpProxyHost ;
7981 return this ;
8082 }
8183
84+ @ Override
8285 public ApacheHttpClientBuilder httpProxyPort (int httpProxyPort ) {
8386 this .httpProxyPort = httpProxyPort ;
8487 return this ;
8588 }
8689
90+ @ Override
8791 public ApacheHttpClientBuilder httpProxyUsername (String httpProxyUsername ) {
8892 this .httpProxyUsername = httpProxyUsername ;
8993 return this ;
9094 }
9195
96+ @ Override
9297 public ApacheHttpClientBuilder httpProxyPassword (String httpProxyPassword ) {
9398 this .httpProxyPassword = httpProxyPassword ;
9499 return this ;
95100 }
96101
102+ @ Override
97103 public ApacheHttpClientBuilder sslConnectionSocketFactory (SSLConnectionSocketFactory sslConnectionSocketFactory ) {
98104 this .sslConnectionSocketFactory = sslConnectionSocketFactory ;
99105 return this ;
100106 }
101107
102108 public IdleConnectionMonitorThread getIdleConnectionMonitorThread () {
103- return idleConnectionMonitorThread ;
109+ return this . idleConnectionMonitorThread ;
104110 }
105111
106112 private void prepare () {
107113 Registry <ConnectionSocketFactory > registry = RegistryBuilder .<ConnectionSocketFactory >create ()
108- .register ("http" , plainConnectionSocketFactory )
109- .register ("https" , sslConnectionSocketFactory )
114+ .register ("http" , this . plainConnectionSocketFactory )
115+ .register ("https" , this . sslConnectionSocketFactory )
110116 .build ();
111- connectionManager = new PoolingHttpClientConnectionManager (registry );
112- connectionManager .setMaxTotal (maxTotalConn );
113- connectionManager .setDefaultMaxPerRoute (maxConnPerHost );
114- connectionManager .setDefaultSocketConfig (
117+ this . connectionManager = new PoolingHttpClientConnectionManager (registry );
118+ this . connectionManager .setMaxTotal (this . maxTotalConn );
119+ this . connectionManager .setDefaultMaxPerRoute (this . maxConnPerHost );
120+ this . connectionManager .setDefaultSocketConfig (
115121 SocketConfig .copy (SocketConfig .DEFAULT )
116- .setSoTimeout (soTimeout )
122+ .setSoTimeout (this . soTimeout )
117123 .build ()
118124 );
119125
120- idleConnectionMonitorThread = new IdleConnectionMonitorThread (connectionManager , idleConnTimeout , checkWaitTime );
121- idleConnectionMonitorThread .setDaemon (true );
122- idleConnectionMonitorThread .start ();
126+ this .idleConnectionMonitorThread = new IdleConnectionMonitorThread (
127+ this .connectionManager , this .idleConnTimeout , this .checkWaitTime );
128+ this .idleConnectionMonitorThread .setDaemon (true );
129+ this .idleConnectionMonitorThread .start ();
123130
124- httpClientBuilder = HttpClients .custom ()
125- .setConnectionManager (connectionManager )
131+ this . httpClientBuilder = HttpClients .custom ()
132+ .setConnectionManager (this . connectionManager )
126133 .setDefaultRequestConfig (
127134 RequestConfig .custom ()
128- .setSocketTimeout (soTimeout )
129- .setConnectTimeout (connectionTimeout )
130- .setConnectionRequestTimeout (connectionRequestTimeout )
135+ .setSocketTimeout (this . soTimeout )
136+ .setConnectTimeout (this . connectionTimeout )
137+ .setConnectionRequestTimeout (this . connectionRequestTimeout )
131138 .build ()
132139 )
133- .setRetryHandler (httpRequestRetryHandler );
140+ .setRetryHandler (this . httpRequestRetryHandler );
134141
135- if (StringUtils .isNotBlank (httpProxyHost ) && StringUtils .isNotBlank (httpProxyUsername )) {
142+ if (StringUtils .isNotBlank (this .httpProxyHost )
143+ && StringUtils .isNotBlank (this .httpProxyUsername )) {
136144 // 使用代理服务器 需要用户认证的代理服务器
137145 CredentialsProvider credsProvider = new BasicCredentialsProvider ();
138146 credsProvider .setCredentials (
139- new AuthScope (httpProxyHost , httpProxyPort ),
140- new UsernamePasswordCredentials (httpProxyUsername , httpProxyPassword ));
141- httpClientBuilder .setDefaultCredentialsProvider (credsProvider );
147+ new AuthScope (this .httpProxyHost , this .httpProxyPort ),
148+ new UsernamePasswordCredentials (this .httpProxyUsername ,
149+ this .httpProxyPassword ));
150+ this .httpClientBuilder .setDefaultCredentialsProvider (credsProvider );
142151 }
143152
144- if (StringUtils .isNotBlank (userAgent )) {
145- httpClientBuilder .setUserAgent (userAgent );
153+ if (StringUtils .isNotBlank (this . userAgent )) {
154+ this . httpClientBuilder .setUserAgent (this . userAgent );
146155 }
147156
148157 }
149158
159+ @ Override
150160 public CloseableHttpClient build () {
151- if (!prepared ) {
161+ if (!this . prepared ) {
152162 prepare ();
153- prepared = true ;
163+ this . prepared = true ;
154164 }
155165
156- return httpClientBuilder .build ();
166+ return this . httpClientBuilder .build ();
157167 }
158168
159169 public static class IdleConnectionMonitorThread extends Thread {
@@ -172,11 +182,12 @@ public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr, int idle
172182 @ Override
173183 public void run () {
174184 try {
175- while (!shutdown ) {
185+ while (!this . shutdown ) {
176186 synchronized (this ) {
177- wait (checkWaitTime );
178- connMgr .closeExpiredConnections ();
179- connMgr .closeIdleConnections (idleConnTimeout , TimeUnit .MILLISECONDS );
187+ wait (this .checkWaitTime );
188+ this .connMgr .closeExpiredConnections ();
189+ this .connMgr .closeIdleConnections (this .idleConnTimeout ,
190+ TimeUnit .MILLISECONDS );
180191 }
181192 }
182193 } catch (InterruptedException ignore ) {
@@ -190,7 +201,7 @@ public void trigger() {
190201 }
191202
192203 public void shutdown () {
193- shutdown = true ;
204+ this . shutdown = true ;
194205 synchronized (this ) {
195206 notifyAll ();
196207 }
0 commit comments