mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-01 00:46:54 +08:00 
			
		
		
		
	🎨 优化ApacheHttpClientBuilder代码
This commit is contained in:
		| @@ -1,5 +1,7 @@ | ||||
| package me.chanjar.weixin.common.util.http.apache; | ||||
|  | ||||
| import org.apache.http.client.HttpRequestRetryHandler; | ||||
| import org.apache.http.conn.ConnectionKeepAliveStrategy; | ||||
| import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | ||||
| import org.apache.http.impl.client.CloseableHttpClient; | ||||
|  | ||||
| @@ -37,6 +39,16 @@ public interface ApacheHttpClientBuilder { | ||||
|    */ | ||||
|   ApacheHttpClientBuilder httpProxyPassword(String httpProxyPassword); | ||||
|  | ||||
|   /** | ||||
|    * 重试策略. | ||||
|    */ | ||||
|   ApacheHttpClientBuilder httpRequestRetryHandler(HttpRequestRetryHandler httpRequestRetryHandler ); | ||||
|  | ||||
|   /** | ||||
|    * 超时时间. | ||||
|    */ | ||||
|   ApacheHttpClientBuilder keepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy); | ||||
|  | ||||
|   /** | ||||
|    * ssl连接socket工厂. | ||||
|    */ | ||||
|   | ||||
| @@ -10,6 +10,7 @@ import org.apache.http.client.config.RequestConfig; | ||||
| import org.apache.http.config.Registry; | ||||
| import org.apache.http.config.RegistryBuilder; | ||||
| import org.apache.http.config.SocketConfig; | ||||
| import org.apache.http.conn.ConnectionKeepAliveStrategy; | ||||
| import org.apache.http.conn.DnsResolver; | ||||
| import org.apache.http.conn.HttpClientConnectionManager; | ||||
| import org.apache.http.conn.socket.ConnectionSocketFactory; | ||||
| @@ -48,12 +49,13 @@ public class ApacheHttpDnsClientBuilder implements ApacheHttpClientBuilder { | ||||
|   private int maxTotalConn = 50; | ||||
|   private String userAgent; | ||||
|  | ||||
|   private DnsResolver dnsResover; | ||||
|   private DnsResolver dnsResolver; | ||||
|  | ||||
|   private HttpRequestRetryHandler httpRequestRetryHandler = (IOException exception, int executionCount, HttpContext context) -> false; | ||||
|   private SSLConnectionSocketFactory sslConnectionSocketFactory = SSLConnectionSocketFactory.getSocketFactory(); | ||||
|   private PlainConnectionSocketFactory plainConnectionSocketFactory = PlainConnectionSocketFactory.getSocketFactory(); | ||||
|   private String httpProxyHost; | ||||
|   private ConnectionKeepAliveStrategy keepAliveStrategy; | ||||
|  | ||||
|   private int httpProxyPort; | ||||
|   private String httpProxyUsername; | ||||
| @@ -97,6 +99,18 @@ public class ApacheHttpDnsClientBuilder implements ApacheHttpClientBuilder { | ||||
|     return this; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public ApacheHttpClientBuilder httpRequestRetryHandler(HttpRequestRetryHandler httpRequestRetryHandler) { | ||||
|     this.httpRequestRetryHandler = httpRequestRetryHandler; | ||||
|     return this; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public ApacheHttpClientBuilder keepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy) { | ||||
|     this.keepAliveStrategy = keepAliveStrategy; | ||||
|     return this; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public ApacheHttpClientBuilder sslConnectionSocketFactory(SSLConnectionSocketFactory sslConnectionSocketFactory) { | ||||
|     this.sslConnectionSocketFactory = sslConnectionSocketFactory; | ||||
| @@ -202,11 +216,11 @@ public class ApacheHttpDnsClientBuilder implements ApacheHttpClientBuilder { | ||||
|  | ||||
|     @SuppressWarnings("resource") | ||||
|     PoolingHttpClientConnectionManager connectionManager; | ||||
|     if (dnsResover != null) { | ||||
|     if (dnsResolver != null) { | ||||
|       if (log.isDebugEnabled()) { | ||||
|         log.debug("specified dns resolver."); | ||||
|       } | ||||
|       connectionManager = new PoolingHttpClientConnectionManager(registry, dnsResover); | ||||
|       connectionManager = new PoolingHttpClientConnectionManager(registry, dnsResolver); | ||||
|     } else { | ||||
|       if (log.isDebugEnabled()) { | ||||
|         log.debug("Not specified dns resolver."); | ||||
| @@ -254,12 +268,12 @@ public class ApacheHttpDnsClientBuilder implements ApacheHttpClientBuilder { | ||||
|     return this.httpClientBuilder.build(); | ||||
|   } | ||||
|  | ||||
|   public DnsResolver getDnsResover() { | ||||
|     return dnsResover; | ||||
|   public DnsResolver getDnsResolver() { | ||||
|     return dnsResolver; | ||||
|   } | ||||
|  | ||||
|   public void setDnsResover(DnsResolver dnsResover) { | ||||
|     this.dnsResover = dnsResover; | ||||
|   public void setDnsResolver(DnsResolver dnsResolver) { | ||||
|     this.dnsResolver = dnsResolver; | ||||
|   } | ||||
|  | ||||
|   public static class IdleConnectionMonitorThread extends Thread { | ||||
|   | ||||
| @@ -103,12 +103,8 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder { | ||||
|    */ | ||||
|   private ConnectionKeepAliveStrategy connectionKeepAliveStrategy; | ||||
|  | ||||
|   private final HttpRequestRetryHandler defaultHttpRequestRetryHandler = new HttpRequestRetryHandler() { | ||||
|     @Override | ||||
|     public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { | ||||
|       return false; | ||||
|     } | ||||
|   }; | ||||
|   private final HttpRequestRetryHandler defaultHttpRequestRetryHandler = (exception, executionCount, context) -> false; | ||||
|  | ||||
|   private SSLConnectionSocketFactory sslConnectionSocketFactory = SSLConnectionSocketFactory.getSocketFactory(); | ||||
|   private final PlainConnectionSocketFactory plainConnectionSocketFactory = PlainConnectionSocketFactory.getSocketFactory(); | ||||
|   private String httpProxyHost; | ||||
| @@ -155,6 +151,18 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder { | ||||
|     return this; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public ApacheHttpClientBuilder httpRequestRetryHandler(HttpRequestRetryHandler httpRequestRetryHandler) { | ||||
|     this.httpRequestRetryHandler = httpRequestRetryHandler; | ||||
|     return this; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public ApacheHttpClientBuilder keepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy) { | ||||
|     this.connectionKeepAliveStrategy = keepAliveStrategy; | ||||
|     return this; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public ApacheHttpClientBuilder sslConnectionSocketFactory(SSLConnectionSocketFactory sslConnectionSocketFactory) { | ||||
|     this.sslConnectionSocketFactory = sslConnectionSocketFactory; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang