private static PoolingHttpClientConnectionManager manager; // 连接池管理类
private static ScheduledExecutorService monitorExecutor = Executors.newScheduledThreadPool(1);;
private static SSLContextBuilder builder = null;
private static SSLConnectionSocketFactory sslSocketFactory = null;
static {
try {
builder = new SSLContextBuilder();
// 全部信任 不做身份鉴定
builder.loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
});
sslSocketFactory = new SSLConnectionSocketFactory(builder.build(), new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE);
}catch(Exception e) {
logger.error("初始化http连接池失败");
e.printStackTrace();
}
ConnectionSocketFactory plainSocketFactory = PlainConnectionSocketFactory.getSocketFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", plainSocketFactory)
.register("https", sslSocketFactory)
.build();
// 创建ConnectionManager,添加Connection配置信息
manager = new PoolingHttpClientConnectionManager(registry);
// 设置连接参数
manager.setMaxTotal(MAX_CONN); // 最大连接数
manager.setDefaultMaxPerRoute(Max_PRE_ROUTE); // 路由最大连接数
// 开启监控线程,对异常和空闲线程进行关闭
monitorExecutor.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
http连接池https忽略证书认证
最新推荐文章于 2024-04-13 00:54:06 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)