com.aliyun.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder Maven / Gradle / Ivy
The newest version!
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* .
*
*/
package com.aliyun.apache.hc.client5.http.impl.io;
import com.aliyun.apache.hc.client5.http.config.ConnectionConfig;
import com.aliyun.apache.hc.client5.http.config.TlsConfig;
import com.aliyun.apache.hc.client5.http.socket.ConnectionSocketFactory;
import com.aliyun.apache.hc.client5.http.socket.LayeredConnectionSocketFactory;
import com.aliyun.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
import com.aliyun.apache.hc.client5.http.DnsResolver;
import com.aliyun.apache.hc.client5.http.HttpRoute;
import com.aliyun.apache.hc.client5.http.SchemePortResolver;
import com.aliyun.apache.hc.client5.http.io.ManagedHttpClientConnection;
import com.aliyun.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import com.aliyun.apache.hc.core5.function.Resolver;
import com.aliyun.apache.hc.core5.http.HttpHost;
import com.aliyun.apache.hc.core5.http.URIScheme;
import com.aliyun.apache.hc.core5.http.config.RegistryBuilder;
import com.aliyun.apache.hc.core5.http.io.HttpConnectionFactory;
import com.aliyun.apache.hc.core5.http.io.SocketConfig;
import com.aliyun.apache.hc.core5.pool.PoolConcurrencyPolicy;
import com.aliyun.apache.hc.core5.pool.PoolReusePolicy;
import com.aliyun.apache.hc.core5.util.TimeValue;
/**
* Builder for {@link PoolingHttpClientConnectionManager} instances.
*
* When a particular component is not explicitly set this class will
* use its default implementation. System properties will be taken
* into account when configuring the default implementations when
* {@link #useSystemProperties()} method is called prior to calling
* {@link #build()}.
*
*
* - ssl.TrustManagerFactory.algorithm
* - javax.net.ssl.trustStoreType
* - javax.net.ssl.trustStore
* - javax.net.ssl.trustStoreProvider
* - javax.net.ssl.trustStorePassword
* - ssl.KeyManagerFactory.algorithm
* - javax.net.ssl.keyStoreType
* - javax.net.ssl.keyStore
* - javax.net.ssl.keyStoreProvider
* - javax.net.ssl.keyStorePassword
* - https.protocols
* - https.cipherSuites
*
*
* @since 5.0
*/
public class PoolingHttpClientConnectionManagerBuilder {
private HttpConnectionFactory connectionFactory;
private LayeredConnectionSocketFactory sslSocketFactory;
private SchemePortResolver schemePortResolver;
private DnsResolver dnsResolver;
private PoolConcurrencyPolicy poolConcurrencyPolicy;
private PoolReusePolicy poolReusePolicy;
private Resolver socketConfigResolver;
private Resolver connectionConfigResolver;
private Resolver tlsConfigResolver;
private boolean systemProperties;
private int maxConnTotal;
private int maxConnPerRoute;
public static PoolingHttpClientConnectionManagerBuilder create() {
return new PoolingHttpClientConnectionManagerBuilder();
}
PoolingHttpClientConnectionManagerBuilder() {
super();
}
/**
* Assigns {@link HttpConnectionFactory} instance.
*/
public final PoolingHttpClientConnectionManagerBuilder setConnectionFactory(
final HttpConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
return this;
}
/**
* Assigns {@link LayeredConnectionSocketFactory} instance.
*/
public final PoolingHttpClientConnectionManagerBuilder setSSLSocketFactory(
final LayeredConnectionSocketFactory sslSocketFactory) {
this.sslSocketFactory = sslSocketFactory;
return this;
}
/**
* Assigns {@link DnsResolver} instance.
*/
public final PoolingHttpClientConnectionManagerBuilder setDnsResolver(final DnsResolver dnsResolver) {
this.dnsResolver = dnsResolver;
return this;
}
/**
* Assigns {@link SchemePortResolver} instance.
*/
public final PoolingHttpClientConnectionManagerBuilder setSchemePortResolver(final SchemePortResolver schemePortResolver) {
this.schemePortResolver = schemePortResolver;
return this;
}
/**
* Assigns {@link PoolConcurrencyPolicy} value.
*/
public final PoolingHttpClientConnectionManagerBuilder setPoolConcurrencyPolicy(final PoolConcurrencyPolicy poolConcurrencyPolicy) {
this.poolConcurrencyPolicy = poolConcurrencyPolicy;
return this;
}
/**
* Assigns {@link PoolReusePolicy} value.
*/
public final PoolingHttpClientConnectionManagerBuilder setConnPoolPolicy(final PoolReusePolicy poolReusePolicy) {
this.poolReusePolicy = poolReusePolicy;
return this;
}
/**
* Assigns maximum total connection value.
*/
public final PoolingHttpClientConnectionManagerBuilder setMaxConnTotal(final int maxConnTotal) {
this.maxConnTotal = maxConnTotal;
return this;
}
/**
* Assigns maximum connection per route value.
*/
public final PoolingHttpClientConnectionManagerBuilder setMaxConnPerRoute(final int maxConnPerRoute) {
this.maxConnPerRoute = maxConnPerRoute;
return this;
}
/**
* Assigns the same {@link SocketConfig} for all routes.
*/
public final PoolingHttpClientConnectionManagerBuilder setDefaultSocketConfig(final SocketConfig config) {
this.socketConfigResolver = (route) -> config;
return this;
}
/**
* Assigns {@link Resolver} of {@link SocketConfig} on a per route basis.
*
* @since 5.2
*/
public final PoolingHttpClientConnectionManagerBuilder setSocketConfigResolver(
final Resolver socketConfigResolver) {
this.socketConfigResolver = socketConfigResolver;
return this;
}
/**
* Assigns the same {@link ConnectionConfig} for all routes.
*
* @since 5.2
*/
public final PoolingHttpClientConnectionManagerBuilder setDefaultConnectionConfig(final ConnectionConfig config) {
this.connectionConfigResolver = (route) -> config;
return this;
}
/**
* Assigns {@link Resolver} of {@link ConnectionConfig} on a per route basis.
*
* @since 5.2
*/
public final PoolingHttpClientConnectionManagerBuilder setConnectionConfigResolver(
final Resolver connectionConfigResolver) {
this.connectionConfigResolver = connectionConfigResolver;
return this;
}
/**
* Assigns the same {@link TlsConfig} for all hosts.
*
* @since 5.2
*/
public final PoolingHttpClientConnectionManagerBuilder setDefaultTlsConfig(final TlsConfig config) {
this.tlsConfigResolver = (host) -> config;
return this;
}
/**
* Assigns {@link Resolver} of {@link TlsConfig} on a per host basis.
*
* @since 5.2
*/
public final PoolingHttpClientConnectionManagerBuilder setTlsConfigResolver(
final Resolver tlsConfigResolver) {
this.tlsConfigResolver = tlsConfigResolver;
return this;
}
/**
* Sets maximum time to live for persistent connections
*
* @deprecated Use {@link #setDefaultConnectionConfig(ConnectionConfig)}.
*/
@Deprecated
public final PoolingHttpClientConnectionManagerBuilder setConnectionTimeToLive(final TimeValue timeToLive) {
setDefaultConnectionConfig(ConnectionConfig.custom()
.setTimeToLive(timeToLive)
.build());
return this;
}
/**
* Sets period after inactivity after which persistent
* connections must be checked to ensure they are still valid.
*
* @deprecated Use {@link #setDefaultConnectionConfig(ConnectionConfig)}.
*/
@Deprecated
public final PoolingHttpClientConnectionManagerBuilder setValidateAfterInactivity(final TimeValue validateAfterInactivity) {
setDefaultConnectionConfig(ConnectionConfig.custom()
.setValidateAfterInactivity(validateAfterInactivity)
.build());
return this;
}
/**
* Use system properties when creating and configuring default
* implementations.
*/
public final PoolingHttpClientConnectionManagerBuilder useSystemProperties() {
this.systemProperties = true;
return this;
}
public PoolingHttpClientConnectionManager build() {
@SuppressWarnings("resource") final PoolingHttpClientConnectionManager poolingmgr = new PoolingHttpClientConnectionManager(
RegistryBuilder.create()
.register(URIScheme.HTTP.id, PlainConnectionSocketFactory.getSocketFactory())
.register(URIScheme.HTTPS.id, sslSocketFactory != null ? sslSocketFactory :
(systemProperties ?
SSLConnectionSocketFactory.getSystemSocketFactory() :
SSLConnectionSocketFactory.getSocketFactory()))
.build(),
poolConcurrencyPolicy,
poolReusePolicy,
null,
schemePortResolver,
dnsResolver,
connectionFactory);
poolingmgr.setSocketConfigResolver(socketConfigResolver);
poolingmgr.setConnectionConfigResolver(connectionConfigResolver);
poolingmgr.setTlsConfigResolver(tlsConfigResolver);
if (maxConnTotal > 0) {
poolingmgr.setMaxTotal(maxConnTotal);
}
if (maxConnPerRoute > 0) {
poolingmgr.setDefaultMaxPerRoute(maxConnPerRoute);
}
return poolingmgr;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy