1
1
package me .chanjar .weixin .common .util .http ;
2
2
3
- import java .io .IOException ;
4
- import java .util .concurrent .TimeUnit ;
5
-
3
+ import me .chanjar .weixin .common .util .StringUtils ;
6
4
import org .apache .http .annotation .NotThreadSafe ;
7
5
import org .apache .http .auth .AuthScope ;
8
6
import org .apache .http .auth .UsernamePasswordCredentials ;
23
21
import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
24
22
import org .apache .http .protocol .HttpContext ;
25
23
26
- import me .chanjar .weixin .common .util .StringUtils ;
24
+ import java .io .IOException ;
25
+ import java .util .concurrent .TimeUnit ;
27
26
28
27
/**
29
28
* httpclient 连接管理器
30
29
*/
31
30
@ NotThreadSafe
32
- public class DefaultApacheHttpHttpClientBuilder implements ApacheHttpClientBuilder {
31
+ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
33
32
private int connectionRequestTimeout = 3000 ;
34
33
private int connectionTimeout = 5000 ;
35
34
private int soTimeout = 5000 ;
@@ -52,27 +51,20 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte
52
51
private String httpProxyUsername ;
53
52
private String httpProxyPassword ;
54
53
55
- /**
56
- * 连接管理器
57
- */
58
- private PoolingHttpClientConnectionManager connectionManager ;
59
54
/**
60
55
* 闲置连接监控线程
61
56
*/
62
57
private IdleConnectionMonitorThread idleConnectionMonitorThread ;
63
58
64
- /**
65
- * httpClientBuilder
66
- */
67
59
private HttpClientBuilder httpClientBuilder ;
68
60
69
61
private boolean prepared = false ;
70
62
71
- private DefaultApacheHttpHttpClientBuilder () {
63
+ private DefaultApacheHttpClientBuilder () {
72
64
}
73
65
74
- public static DefaultApacheHttpHttpClientBuilder get () {
75
- return new DefaultApacheHttpHttpClientBuilder ();
66
+ public static DefaultApacheHttpClientBuilder get () {
67
+ return new DefaultApacheHttpClientBuilder ();
76
68
}
77
69
78
70
@ Override
@@ -111,43 +103,44 @@ public IdleConnectionMonitorThread getIdleConnectionMonitorThread() {
111
103
112
104
private void prepare () {
113
105
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 )
116
108
.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 (
121
114
SocketConfig .copy (SocketConfig .DEFAULT )
122
- .setSoTimeout (this .soTimeout )
115
+ .setSoTimeout (this .soTimeout )
123
116
.build ()
124
117
);
125
118
126
119
this .idleConnectionMonitorThread = new IdleConnectionMonitorThread (
127
- this . connectionManager , this .idleConnTimeout , this .checkWaitTime );
120
+ connectionManager , this .idleConnTimeout , this .checkWaitTime );
128
121
this .idleConnectionMonitorThread .setDaemon (true );
129
122
this .idleConnectionMonitorThread .start ();
130
123
131
124
this .httpClientBuilder = HttpClients .custom ()
132
- .setConnectionManager (this . connectionManager )
125
+ .setConnectionManager (connectionManager )
133
126
.setDefaultRequestConfig (
134
127
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 )
138
131
.build ()
139
132
)
140
- .setRetryHandler (this .httpRequestRetryHandler );
133
+ .setRetryHandler (this .httpRequestRetryHandler );
141
134
142
135
if (StringUtils .isNotBlank (this .httpProxyHost )
143
- && StringUtils .isNotBlank (this .httpProxyUsername )) {
136
+ && StringUtils .isNotBlank (this .httpProxyUsername )) {
144
137
// 使用代理服务器 需要用户认证的代理服务器
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 );
151
144
}
152
145
153
146
if (StringUtils .isNotBlank (this .userAgent )) {
@@ -187,7 +180,7 @@ public void run() {
187
180
wait (this .checkWaitTime );
188
181
this .connMgr .closeExpiredConnections ();
189
182
this .connMgr .closeIdleConnections (this .idleConnTimeout ,
190
- TimeUnit .MILLISECONDS );
183
+ TimeUnit .MILLISECONDS );
191
184
}
192
185
}
193
186
} catch (InterruptedException ignore ) {
0 commit comments