mirror of
https://gitee.com/egzosn/pay-java-parent.git
synced 2026-05-12 16:37:31 +08:00
http连接池
This commit is contained in:
@@ -32,11 +32,22 @@ public class HttpConfigStorage {
|
||||
*/
|
||||
private boolean isPath = true;
|
||||
|
||||
//https请求所需的证书(PKCS12)
|
||||
/**
|
||||
* https请求所需的证书(PKCS12)
|
||||
*/
|
||||
private String keystore;
|
||||
//证书对应的密码
|
||||
/**
|
||||
* 证书对应的密码
|
||||
*/
|
||||
private String storePassword;
|
||||
|
||||
/**
|
||||
* 最大连接数
|
||||
*/
|
||||
private int maxTotal = 0;
|
||||
/**
|
||||
* 默认的每个路由的最大连接数
|
||||
*/
|
||||
private int defaultMaxPerRoute = 0;
|
||||
|
||||
/**
|
||||
* http代理地址
|
||||
@@ -190,4 +201,20 @@ public class HttpConfigStorage {
|
||||
public void setStorePassword(String storePassword) {
|
||||
this.storePassword = storePassword;
|
||||
}
|
||||
|
||||
public int getMaxTotal() {
|
||||
return maxTotal;
|
||||
}
|
||||
|
||||
public void setMaxTotal(int maxTotal) {
|
||||
this.maxTotal = maxTotal;
|
||||
}
|
||||
|
||||
public int getDefaultMaxPerRoute() {
|
||||
return defaultMaxPerRoute;
|
||||
}
|
||||
|
||||
public void setDefaultMaxPerRoute(int defaultMaxPerRoute) {
|
||||
this.defaultMaxPerRoute = defaultMaxPerRoute;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,11 +8,16 @@ import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.config.Registry;
|
||||
import org.apache.http.config.RegistryBuilder;
|
||||
import org.apache.http.conn.socket.ConnectionSocketFactory;
|
||||
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.ssl.SSLContexts;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
@@ -35,8 +40,11 @@ public class HttpRequestTemplate {
|
||||
|
||||
protected CloseableHttpClient httpClient;
|
||||
|
||||
protected PoolingHttpClientConnectionManager connectionManager;
|
||||
|
||||
protected HttpHost httpProxy;
|
||||
|
||||
HttpConfigStorage configStorage;
|
||||
/**
|
||||
* 获取代理带代理地址的 HttpHost
|
||||
* @return 获取代理带代理地址的 HttpHost
|
||||
@@ -46,7 +54,27 @@ public class HttpRequestTemplate {
|
||||
}
|
||||
|
||||
public CloseableHttpClient getHttpClient() {
|
||||
if (null != httpClient) {
|
||||
return httpClient;
|
||||
}
|
||||
if (null == configStorage) {
|
||||
return httpClient = HttpClients.createDefault();
|
||||
}
|
||||
|
||||
CloseableHttpClient httpClient = HttpClients
|
||||
.custom()
|
||||
//网络提供者
|
||||
.setDefaultCredentialsProvider(createCredentialsProvider(configStorage))
|
||||
//设置httpclient的SSLSocketFactory
|
||||
.setSSLSocketFactory(createSSL(configStorage))
|
||||
.setConnectionManager(connectionManager(configStorage))
|
||||
.build();
|
||||
if (null == connectionManager) {
|
||||
return this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
return httpClient;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,6 +154,28 @@ public class HttpRequestTemplate {
|
||||
return credsProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化连接池
|
||||
* @param configStorage 配置
|
||||
* @return 连接池对象
|
||||
*/
|
||||
public PoolingHttpClientConnectionManager connectionManager(HttpConfigStorage configStorage){
|
||||
if (null != connectionManager){
|
||||
return connectionManager;
|
||||
}
|
||||
if (0 == configStorage.getMaxTotal() || 0 == configStorage.getDefaultMaxPerRoute()){
|
||||
return null;
|
||||
}
|
||||
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
|
||||
.register("https", createSSL(configStorage))
|
||||
.register("http", new PlainConnectionSocketFactory())
|
||||
.build();
|
||||
connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
|
||||
connectionManager.setMaxTotal(configStorage.getMaxTotal());
|
||||
connectionManager.setDefaultMaxPerRoute(configStorage.getDefaultMaxPerRoute());
|
||||
|
||||
return connectionManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置HTTP请求的配置
|
||||
@@ -134,24 +184,13 @@ public class HttpRequestTemplate {
|
||||
* @return 当前HTTP请求的客户端模板
|
||||
*/
|
||||
public HttpRequestTemplate setHttpConfigStorage(HttpConfigStorage configStorage) {
|
||||
|
||||
if (null == configStorage) {
|
||||
httpClient = HttpClients.createDefault();
|
||||
return this;
|
||||
}
|
||||
|
||||
httpClient = HttpClients
|
||||
.custom()
|
||||
//网络提供者
|
||||
.setDefaultCredentialsProvider(createCredentialsProvider(configStorage))
|
||||
//设置httpclient的SSLSocketFactory
|
||||
.setSSLSocketFactory(createSSL(configStorage))
|
||||
.build();
|
||||
|
||||
this.configStorage = configStorage;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* post
|
||||
@@ -275,7 +314,7 @@ public class HttpRequestTemplate {
|
||||
httpRequest.setProxy(httpProxy);
|
||||
}
|
||||
httpRequest.setResponseType(responseType);
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpRequest)) {
|
||||
try (CloseableHttpResponse response = getHttpClient().execute(httpRequest)) {
|
||||
return httpRequest.handleResponse(response);
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
|
||||
Reference in New Issue
Block a user