|
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 连接管理器 |
@@ -104,44 +103,45 @@ public IdleConnectionMonitorThread getIdleConnectionMonitorThread() { |
104 | 103 |
|
105 | 104 | private void prepare() { |
106 | 105 | Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() |
107 | | - .register("http", this.plainConnectionSocketFactory) |
108 | | - .register("https", this.sslConnectionSocketFactory) |
109 | | - .build(); |
| 106 | + .register("http", this.plainConnectionSocketFactory) |
| 107 | + .register("https", this.sslConnectionSocketFactory) |
| 108 | + .build(); |
110 | 109 |
|
111 | 110 | @SuppressWarnings("resource") |
112 | 111 | PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry); |
113 | 112 | connectionManager.setMaxTotal(this.maxTotalConn); |
114 | 113 | connectionManager.setDefaultMaxPerRoute(this.maxConnPerHost); |
115 | 114 | connectionManager.setDefaultSocketConfig( |
116 | | - SocketConfig.copy(SocketConfig.DEFAULT) |
117 | | - .setSoTimeout(this.soTimeout) |
118 | | - .build() |
| 115 | + SocketConfig.copy(SocketConfig.DEFAULT) |
| 116 | + .setSoTimeout(this.soTimeout) |
| 117 | + .build() |
119 | 118 | ); |
120 | 119 |
|
121 | 120 | this.idleConnectionMonitorThread = new IdleConnectionMonitorThread( |
122 | | - connectionManager, this.idleConnTimeout, this.checkWaitTime); |
| 121 | + connectionManager, this.idleConnTimeout, this.checkWaitTime); |
123 | 122 | this.idleConnectionMonitorThread.setDaemon(true); |
124 | 123 | this.idleConnectionMonitorThread.start(); |
125 | 124 |
|
126 | 125 | this.httpClientBuilder = HttpClients.custom() |
127 | | - .setConnectionManager(connectionManager) |
128 | | - .setDefaultRequestConfig( |
129 | | - RequestConfig.custom() |
130 | | - .setSocketTimeout(this.soTimeout) |
131 | | - .setConnectTimeout(this.connectionTimeout) |
132 | | - .setConnectionRequestTimeout(this.connectionRequestTimeout) |
133 | | - .build() |
134 | | - ) |
135 | | - .setRetryHandler(this.httpRequestRetryHandler); |
| 126 | + .setConnectionManager(connectionManager) |
| 127 | + .setConnectionManagerShared(true) |
| 128 | + .setDefaultRequestConfig( |
| 129 | + RequestConfig.custom() |
| 130 | + .setSocketTimeout(this.soTimeout) |
| 131 | + .setConnectTimeout(this.connectionTimeout) |
| 132 | + .setConnectionRequestTimeout(this.connectionRequestTimeout) |
| 133 | + .build() |
| 134 | + ) |
| 135 | + .setRetryHandler(this.httpRequestRetryHandler); |
136 | 136 |
|
137 | 137 | if (StringUtils.isNotBlank(this.httpProxyHost) |
138 | | - && StringUtils.isNotBlank(this.httpProxyUsername)) { |
| 138 | + && StringUtils.isNotBlank(this.httpProxyUsername)) { |
139 | 139 | // 使用代理服务器 需要用户认证的代理服务器 |
140 | 140 | CredentialsProvider provider = new BasicCredentialsProvider(); |
141 | 141 | provider.setCredentials( |
142 | | - new AuthScope(this.httpProxyHost, this.httpProxyPort), |
143 | | - new UsernamePasswordCredentials(this.httpProxyUsername, |
144 | | - this.httpProxyPassword)); |
| 142 | + new AuthScope(this.httpProxyHost, this.httpProxyPort), |
| 143 | + new UsernamePasswordCredentials(this.httpProxyUsername, |
| 144 | + this.httpProxyPassword)); |
145 | 145 | this.httpClientBuilder.setDefaultCredentialsProvider(provider); |
146 | 146 | } |
147 | 147 |
|
@@ -182,7 +182,7 @@ public void run() { |
182 | 182 | wait(this.checkWaitTime); |
183 | 183 | this.connMgr.closeExpiredConnections(); |
184 | 184 | this.connMgr.closeIdleConnections(this.idleConnTimeout, |
185 | | - TimeUnit.MILLISECONDS); |
| 185 | + TimeUnit.MILLISECONDS); |
186 | 186 | } |
187 | 187 | } |
188 | 188 | } catch (InterruptedException ignore) { |
|
0 commit comments