|
| 1 | +package me.chanjar.weixin.common.util.http; |
| 2 | + |
| 3 | +import me.chanjar.weixin.common.util.StringUtils; |
| 4 | +import org.apache.http.annotation.NotThreadSafe; |
| 5 | +import org.apache.http.auth.AuthScope; |
| 6 | +import org.apache.http.auth.UsernamePasswordCredentials; |
| 7 | +import org.apache.http.client.CredentialsProvider; |
| 8 | +import org.apache.http.client.HttpRequestRetryHandler; |
| 9 | +import org.apache.http.client.config.RequestConfig; |
| 10 | +import org.apache.http.config.Registry; |
| 11 | +import org.apache.http.config.RegistryBuilder; |
| 12 | +import org.apache.http.config.SocketConfig; |
| 13 | +import org.apache.http.conn.HttpClientConnectionManager; |
| 14 | +import org.apache.http.conn.socket.ConnectionSocketFactory; |
| 15 | +import org.apache.http.conn.socket.PlainConnectionSocketFactory; |
| 16 | +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
| 17 | +import org.apache.http.impl.client.BasicCredentialsProvider; |
| 18 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 19 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 20 | +import org.apache.http.impl.client.HttpClients; |
| 21 | +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
| 22 | +import org.apache.http.protocol.HttpContext; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | + |
| 27 | +/** |
| 28 | + * httpclient 连接管理器 |
| 29 | + */ |
| 30 | +@NotThreadSafe |
| 31 | +public class DefaultApacheHttpHttpClientBuilder implements ApacheHttpClientBuilder { |
| 32 | + private int connectionRequestTimeout = 3000; |
| 33 | + private int connectionTimeout = 5000; |
| 34 | + private int soTimeout = 5000; |
| 35 | + private int idleConnTimeout = 60000; |
| 36 | + private int checkWaitTime = 5000; |
| 37 | + private int maxConnPerHost = 10; |
| 38 | + private int maxTotalConn = 50; |
| 39 | + private String userAgent; |
| 40 | + private HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() { |
| 41 | + @Override |
| 42 | + public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + }; |
| 46 | + private SSLConnectionSocketFactory sslConnectionSocketFactory = SSLConnectionSocketFactory.getSocketFactory(); |
| 47 | + private PlainConnectionSocketFactory plainConnectionSocketFactory = PlainConnectionSocketFactory.getSocketFactory(); |
| 48 | + |
| 49 | + private String httpProxyHost; |
| 50 | + private int httpProxyPort; |
| 51 | + private String httpProxyUsername; |
| 52 | + private String httpProxyPassword; |
| 53 | + |
| 54 | + /** |
| 55 | + * 连接管理器 |
| 56 | + */ |
| 57 | + private PoolingHttpClientConnectionManager connectionManager; |
| 58 | + /** |
| 59 | + * 闲置连接监控线程 |
| 60 | + */ |
| 61 | + private IdleConnectionMonitorThread idleConnectionMonitorThread; |
| 62 | + |
| 63 | + /** |
| 64 | + * httpClientBuilder |
| 65 | + */ |
| 66 | + private HttpClientBuilder httpClientBuilder; |
| 67 | + |
| 68 | + private boolean prepared = false; |
| 69 | + |
| 70 | + private DefaultApacheHttpHttpClientBuilder() { |
| 71 | + } |
| 72 | + |
| 73 | + public static DefaultApacheHttpHttpClientBuilder get() { |
| 74 | + return new DefaultApacheHttpHttpClientBuilder(); |
| 75 | + } |
| 76 | + |
| 77 | + public ApacheHttpClientBuilder httpProxyHost(String httpProxyHost) { |
| 78 | + this.httpProxyHost = httpProxyHost; |
| 79 | + return this; |
| 80 | + } |
| 81 | + |
| 82 | + public ApacheHttpClientBuilder httpProxyPort(int httpProxyPort) { |
| 83 | + this.httpProxyPort = httpProxyPort; |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + public ApacheHttpClientBuilder httpProxyUsername(String httpProxyUsername) { |
| 88 | + this.httpProxyUsername = httpProxyUsername; |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + public ApacheHttpClientBuilder httpProxyPassword(String httpProxyPassword) { |
| 93 | + this.httpProxyPassword = httpProxyPassword; |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + public ApacheHttpClientBuilder sslConnectionSocketFactory(SSLConnectionSocketFactory sslConnectionSocketFactory){ |
| 98 | + this.sslConnectionSocketFactory = sslConnectionSocketFactory; |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + public IdleConnectionMonitorThread getIdleConnectionMonitorThread() { |
| 103 | + return idleConnectionMonitorThread; |
| 104 | + } |
| 105 | + |
| 106 | + private void prepare(){ |
| 107 | + Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() |
| 108 | + .register("http", plainConnectionSocketFactory) |
| 109 | + .register("https", sslConnectionSocketFactory) |
| 110 | + .build(); |
| 111 | + connectionManager = new PoolingHttpClientConnectionManager(registry); |
| 112 | + connectionManager.setMaxTotal(maxTotalConn); |
| 113 | + connectionManager.setDefaultMaxPerRoute(maxConnPerHost); |
| 114 | + connectionManager.setDefaultSocketConfig( |
| 115 | + SocketConfig.copy(SocketConfig.DEFAULT) |
| 116 | + .setSoTimeout(soTimeout) |
| 117 | + .build() |
| 118 | + ); |
| 119 | + |
| 120 | + idleConnectionMonitorThread = new IdleConnectionMonitorThread(connectionManager, idleConnTimeout, checkWaitTime); |
| 121 | + idleConnectionMonitorThread.setDaemon(true); |
| 122 | + idleConnectionMonitorThread.start(); |
| 123 | + |
| 124 | + httpClientBuilder = HttpClients.custom() |
| 125 | + .setConnectionManager(connectionManager) |
| 126 | + .setDefaultRequestConfig( |
| 127 | + RequestConfig.custom() |
| 128 | + .setSocketTimeout(soTimeout) |
| 129 | + .setConnectTimeout(connectionTimeout) |
| 130 | + .setConnectionRequestTimeout(connectionRequestTimeout) |
| 131 | + .build() |
| 132 | + ) |
| 133 | + .setRetryHandler(httpRequestRetryHandler); |
| 134 | + |
| 135 | + if (StringUtils.isNotBlank(httpProxyHost) && StringUtils.isNotBlank(httpProxyUsername)) { |
| 136 | + // 使用代理服务器 需要用户认证的代理服务器 |
| 137 | + CredentialsProvider credsProvider = new BasicCredentialsProvider(); |
| 138 | + credsProvider.setCredentials( |
| 139 | + new AuthScope(httpProxyHost, httpProxyPort), |
| 140 | + new UsernamePasswordCredentials(httpProxyUsername, httpProxyPassword)); |
| 141 | + httpClientBuilder.setDefaultCredentialsProvider(credsProvider); |
| 142 | + } |
| 143 | + |
| 144 | + if (StringUtils.isNotBlank(userAgent)) { |
| 145 | + httpClientBuilder.setUserAgent(userAgent); |
| 146 | + } |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + public CloseableHttpClient build() { |
| 151 | + if(!prepared){ |
| 152 | + prepare(); |
| 153 | + prepared = true; |
| 154 | + } |
| 155 | + |
| 156 | + return httpClientBuilder.build(); |
| 157 | + } |
| 158 | + |
| 159 | + public static class IdleConnectionMonitorThread extends Thread { |
| 160 | + private final HttpClientConnectionManager connMgr; |
| 161 | + private final int idleConnTimeout; |
| 162 | + private final int checkWaitTime; |
| 163 | + private volatile boolean shutdown; |
| 164 | + |
| 165 | + public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr, int idleConnTimeout, int checkWaitTime) { |
| 166 | + super("IdleConnectionMonitorThread"); |
| 167 | + this.connMgr = connMgr; |
| 168 | + this.idleConnTimeout = idleConnTimeout; |
| 169 | + this.checkWaitTime = checkWaitTime; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public void run() { |
| 174 | + try { |
| 175 | + while (!shutdown) { |
| 176 | + synchronized (this) { |
| 177 | + wait(checkWaitTime); |
| 178 | + connMgr.closeExpiredConnections(); |
| 179 | + connMgr.closeIdleConnections(idleConnTimeout, TimeUnit.MILLISECONDS); |
| 180 | + } |
| 181 | + } |
| 182 | + } catch (InterruptedException ignore) { |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + public void trigger() { |
| 187 | + synchronized (this) { |
| 188 | + notifyAll(); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + public void shutdown() { |
| 193 | + shutdown = true; |
| 194 | + synchronized (this) { |
| 195 | + notifyAll(); |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments