1
1
package me .chanjar .weixin .common .util .http ;
2
2
3
- import me .chanjar .weixin .common .util .StringUtils ;
3
+ import java .io .IOException ;
4
+ import java .util .concurrent .TimeUnit ;
5
+
4
6
import org .apache .http .annotation .NotThreadSafe ;
5
7
import org .apache .http .auth .AuthScope ;
6
8
import org .apache .http .auth .UsernamePasswordCredentials ;
21
23
import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
22
24
import org .apache .http .protocol .HttpContext ;
23
25
24
- import java .io .IOException ;
25
- import java .util .concurrent .TimeUnit ;
26
+ import me .chanjar .weixin .common .util .StringUtils ;
26
27
27
28
/**
28
29
* httpclient 连接管理器
@@ -33,7 +34,7 @@ public class DefaultApacheHttpHttpClientBuilder implements ApacheHttpClientBuild
33
34
private int connectionTimeout = 5000 ;
34
35
private int soTimeout = 5000 ;
35
36
private int idleConnTimeout = 60000 ;
36
- private int checkWaitTime = 5000 ;
37
+ private int checkWaitTime = 60000 ;
37
38
private int maxConnPerHost = 10 ;
38
39
private int maxTotalConn = 50 ;
39
40
private String userAgent ;
@@ -74,86 +75,95 @@ public static DefaultApacheHttpHttpClientBuilder get() {
74
75
return new DefaultApacheHttpHttpClientBuilder ();
75
76
}
76
77
78
+ @ Override
77
79
public ApacheHttpClientBuilder httpProxyHost (String httpProxyHost ) {
78
80
this .httpProxyHost = httpProxyHost ;
79
81
return this ;
80
82
}
81
83
84
+ @ Override
82
85
public ApacheHttpClientBuilder httpProxyPort (int httpProxyPort ) {
83
86
this .httpProxyPort = httpProxyPort ;
84
87
return this ;
85
88
}
86
89
90
+ @ Override
87
91
public ApacheHttpClientBuilder httpProxyUsername (String httpProxyUsername ) {
88
92
this .httpProxyUsername = httpProxyUsername ;
89
93
return this ;
90
94
}
91
95
96
+ @ Override
92
97
public ApacheHttpClientBuilder httpProxyPassword (String httpProxyPassword ) {
93
98
this .httpProxyPassword = httpProxyPassword ;
94
99
return this ;
95
100
}
96
101
102
+ @ Override
97
103
public ApacheHttpClientBuilder sslConnectionSocketFactory (SSLConnectionSocketFactory sslConnectionSocketFactory ) {
98
104
this .sslConnectionSocketFactory = sslConnectionSocketFactory ;
99
105
return this ;
100
106
}
101
107
102
108
public IdleConnectionMonitorThread getIdleConnectionMonitorThread () {
103
- return idleConnectionMonitorThread ;
109
+ return this . idleConnectionMonitorThread ;
104
110
}
105
111
106
112
private void prepare () {
107
113
Registry <ConnectionSocketFactory > registry = RegistryBuilder .<ConnectionSocketFactory >create ()
108
- .register ("http" , plainConnectionSocketFactory )
109
- .register ("https" , sslConnectionSocketFactory )
114
+ .register ("http" , this . plainConnectionSocketFactory )
115
+ .register ("https" , this . sslConnectionSocketFactory )
110
116
.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 (
115
121
SocketConfig .copy (SocketConfig .DEFAULT )
116
- .setSoTimeout (soTimeout )
122
+ .setSoTimeout (this . soTimeout )
117
123
.build ()
118
124
);
119
125
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 ();
123
130
124
- httpClientBuilder = HttpClients .custom ()
125
- .setConnectionManager (connectionManager )
131
+ this . httpClientBuilder = HttpClients .custom ()
132
+ .setConnectionManager (this . connectionManager )
126
133
.setDefaultRequestConfig (
127
134
RequestConfig .custom ()
128
- .setSocketTimeout (soTimeout )
129
- .setConnectTimeout (connectionTimeout )
130
- .setConnectionRequestTimeout (connectionRequestTimeout )
135
+ .setSocketTimeout (this . soTimeout )
136
+ .setConnectTimeout (this . connectionTimeout )
137
+ .setConnectionRequestTimeout (this . connectionRequestTimeout )
131
138
.build ()
132
139
)
133
- .setRetryHandler (httpRequestRetryHandler );
140
+ .setRetryHandler (this . httpRequestRetryHandler );
134
141
135
- if (StringUtils .isNotBlank (httpProxyHost ) && StringUtils .isNotBlank (httpProxyUsername )) {
142
+ if (StringUtils .isNotBlank (this .httpProxyHost )
143
+ && StringUtils .isNotBlank (this .httpProxyUsername )) {
136
144
// 使用代理服务器 需要用户认证的代理服务器
137
145
CredentialsProvider credsProvider = new BasicCredentialsProvider ();
138
146
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 );
142
151
}
143
152
144
- if (StringUtils .isNotBlank (userAgent )) {
145
- httpClientBuilder .setUserAgent (userAgent );
153
+ if (StringUtils .isNotBlank (this . userAgent )) {
154
+ this . httpClientBuilder .setUserAgent (this . userAgent );
146
155
}
147
156
148
157
}
149
158
159
+ @ Override
150
160
public CloseableHttpClient build () {
151
- if (!prepared ) {
161
+ if (!this . prepared ) {
152
162
prepare ();
153
- prepared = true ;
163
+ this . prepared = true ;
154
164
}
155
165
156
- return httpClientBuilder .build ();
166
+ return this . httpClientBuilder .build ();
157
167
}
158
168
159
169
public static class IdleConnectionMonitorThread extends Thread {
@@ -172,11 +182,12 @@ public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr, int idle
172
182
@ Override
173
183
public void run () {
174
184
try {
175
- while (!shutdown ) {
185
+ while (!this . shutdown ) {
176
186
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 );
180
191
}
181
192
}
182
193
} catch (InterruptedException ignore ) {
@@ -190,7 +201,7 @@ public void trigger() {
190
201
}
191
202
192
203
public void shutdown () {
193
- shutdown = true ;
204
+ this . shutdown = true ;
194
205
synchronized (this ) {
195
206
notifyAll ();
196
207
}
0 commit comments