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