下面列出了怎么用org.apache.http.conn.HttpClientConnectionManager的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void freeManager(HTTPMethod method) {
synchronized (this) {
HttpClientConnectionManager mgr = methodmap.get(method);
if (mgr == null)
throw new IllegalStateException();
mgrmap.remove(mgr, method);
methodmap.remove(method);
((BasicHttpClientConnectionManager) mgr).close();
if (method.executed) {
// only decrement if method was executed
this.actualconnections--;
}
if (TRACE)
System.err.println("HTTPConnections: close connection: " + method.hashCode());
}
}
private HttpClientConnectionManager createConnectionManager(final Http4FileSystemConfigBuilder builder,
final FileSystemOptions fileSystemOptions) throws FileSystemException {
final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setMaxTotal(builder.getMaxTotalConnections(fileSystemOptions));
connManager.setDefaultMaxPerRoute(builder.getMaxConnectionsPerHost(fileSystemOptions));
final SocketConfig socketConfig =
SocketConfig
.custom()
.setSoTimeout(builder.getSoTimeout(fileSystemOptions))
.build();
connManager.setDefaultSocketConfig(socketConfig);
return connManager;
}
private static void initEvictorThread() {
if (thread == null) {
thread = new Thread(() -> {
try {
while (!Thread.currentThread().isInterrupted()) {
Thread.sleep(sleepTimeMs);
synchronized (connectionManagers) {
// iterate over connection managers
for (HttpClientConnectionManager connectionManager : connectionManagers) {
connectionManager.closeExpiredConnections();
if (maxIdleTimeMs > 0) {
connectionManager.closeIdleConnections(maxIdleTimeMs, TimeUnit.MILLISECONDS);
}
}
}
}
} catch (final Exception ex) {
LOGGER.error(ex);
}
}, "Connection evictor");
thread.setDaemon(true);
}
}
private CloseableHttpClient createHttpClient(HttpClientConnectionManager connectionManager) {
//HttpHost proxy = new HttpHost("127.0.0.1",8889);
RequestConfig requestConfig = RequestConfig.custom()
//.setProxy(proxy)
.setConnectionRequestTimeout(httpClientConfig.getConnectionRequestTimeout())// 获取连接等待时间
.setConnectTimeout(httpClientConfig.getConnectTimeout())// 连接超时
.setSocketTimeout(httpClientConfig.getSocketTimeout())// 获取数据超时
.build();
httpClient = HttpClients.custom().setConnectionManager(httpClientConnectionManager)
.setDefaultRequestConfig(requestConfig)
.setUserAgent(httpClientConfig.getUserAgent())
.disableContentCompression().disableAutomaticRetries()
.setConnectionTimeToLive(httpClientConfig.getConnTimeToLive(), TimeUnit.MILLISECONDS)// 连接最大存活时间
.setRetryHandler(new DefaultHttpRequestRetryHandler(httpClientConfig.getRetyTimes(), true))// 重试次数
.build();
return httpClient;
}
/**
* Returns a wrapped instance of {@link HttpClientConnectionManager}
* to capture the necessary performance metrics.
*
* @param orig the target instance to be wrapped
*/
public static HttpClientConnectionManager wrap(HttpClientConnectionManager orig) {
if (orig instanceof Wrapped) {
throw new IllegalArgumentException();
}
Class<?>[] interfaces;
if (orig instanceof ConnPoolControl) {
interfaces = new Class<?>[]{
HttpClientConnectionManager.class,
ConnPoolControl.class,
Wrapped.class};
} else {
interfaces = new Class<?>[]{
HttpClientConnectionManager.class,
Wrapped.class
};
}
return (HttpClientConnectionManager) Proxy.newProxyInstance(
// https://github.com/aws/aws-sdk-java/pull/48#issuecomment-29454423
ClientConnectionManagerFactory.class.getClassLoader(),
interfaces,
new Handler(orig));
}
private static HttpClient buildHttpClient(HttpClientConnectionManager connectionManager)
{
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(1500)
.setConnectionRequestTimeout(1500)
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(requestConfig)
// use the default retry handler:
// https://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e305
.build();
return httpClient;
}
private Pair<CloseableHttpClient,HttpClientContext> getClient() {
HttpClientConnectionManager pool = new PoolingHttpClientConnectionManager();
HttpHost host = new HttpHost("localhost", REST_TEST.getServletPort());
Registry<AuthSchemeProvider> authRegistry =
RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO,
new SPNegoSchemeFactory(true, true)).build();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, EmptyCredentials.INSTANCE);
AuthCache authCache = new BasicAuthCache();
CloseableHttpClient client = HttpClients.custom()
.setDefaultAuthSchemeRegistry(authRegistry)
.setConnectionManager(pool).build();
HttpClientContext context = HttpClientContext.create();
context.setTargetHost(host);
context.setCredentialsProvider(credentialsProvider);
context.setAuthSchemeRegistry(authRegistry);
context.setAuthCache(authCache);
return new Pair<>(client, context);
}
@Override
public HttpClientConnectionManager create(final HttpClientSettings settings) {
ConnectionSocketFactory sslsf = getPreferredSocketFactory(settings);
final PoolingHttpClientConnectionManager cm = new
PoolingHttpClientConnectionManager(
createSocketFactoryRegistry(sslsf),
null,
DefaultSchemePortResolver.INSTANCE,
new DelegatingDnsResolver(settings.getDnsResolver()),
settings.getConnectionPoolTTL(),
TimeUnit.MILLISECONDS);
cm.setValidateAfterInactivity(settings.getValidateAfterInactivityMillis());
cm.setDefaultMaxPerRoute(settings.getMaxConnections());
cm.setMaxTotal(settings.getMaxConnections());
cm.setDefaultSocketConfig(buildSocketConfig(settings));
cm.setDefaultConnectionConfig(buildConnectionConfig(settings));
return cm;
}
public static CloseableHttpClient create(HttpClientConnectionManager connectionManager, OnrampOptions options) {
return org.apache.http.impl.client.HttpClientBuilder
.create()
.setUserAgent(userAgent())
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(requestConfig())
.setConnectionReuseStrategy(DefaultClientConnectionReuseStrategy.INSTANCE)
.setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE)
.setRetryHandler(getRetryHandler(options))
.setServiceUnavailableRetryStrategy(new DefaultServiceUnavailableRetryStrategy(1, 500))
.build();
}
@Test
public void newConnectionManagerWithDisabledSSLValidation() throws Exception {
HttpClientConnectionManager connectionManager = new DefaultApacheHttpClientConnectionManagerFactory()
.newConnectionManager(true, 2, 6);
Lookup<ConnectionSocketFactory> socketFactoryRegistry = getConnectionSocketFactoryLookup(
connectionManager);
then(socketFactoryRegistry.lookup("https")).isNotNull();
then(getX509TrustManager(socketFactoryRegistry).getAcceptedIssuers()).isNull();
}
@Override
public HttpClientConnectionManager newManager(HTTPMethod m) {
synchronized (this) {
if (TRACE)
System.err.println("HTTPConnections: open connection: " + m.hashCode());
this.actualconnections++;
return getPool();
}
}
/**
* Create http client based on connection manager.
*
* @param connectionManager The connection manager setting http client.
* @return Http client based on connection manager.
*/
private CloseableHttpClient createHttpClient(HttpClientConnectionManager connectionManager) {
HttpClientBuilder builder =
HttpClients.custom().setConnectionManager(connectionManager).disableAutomaticRetries();
int socketBufferSizeInBytes = this.config.getSocketBufferSizeInBytes();
if (socketBufferSizeInBytes > 0) {
builder.setDefaultConnectionConfig(
ConnectionConfig.custom().setBufferSize(socketBufferSizeInBytes).build());
}
return builder.build();
}
@Test
void testGetAndSetConnectionManager()
{
HttpClientConnectionManager connectionManager = mock(HttpClientConnectionManager.class);
config.setConnectionManager(connectionManager);
assertEquals(connectionManager, config.getConnectionManager());
}
private static HttpClientConnectionManager getPooledConnectionFactory(String certPath, int maxConnections) throws IOException {
PoolingHttpClientConnectionManager ret = certPath != null ?
new PoolingHttpClientConnectionManager(getSslFactoryRegistry(certPath)) :
new PoolingHttpClientConnectionManager();
ret.setDefaultMaxPerRoute(maxConnections);
ret.setMaxTotal(maxConnections);
return ret;
}
@Override
public final JestClient getObject() {
// no other way than copying almost whole super.getObject() ..
BufferedJestHttpClient client = createDefaultClient();
HttpClientConfig httpClientConfig = wrappedHttpClientConfig.getHttpClientConfig();
client.setServers(httpClientConfig.getServerList());
final HttpClientConnectionManager connectionManager = getConnectionManager();
client.setHttpClient(createHttpClient(connectionManager));
final NHttpClientConnectionManager asyncConnectionManager = getAsyncConnectionManager();
client.setAsyncClient(createAsyncHttpClient(asyncConnectionManager));
// schedule idle connection reaping if configured
if (httpClientConfig.getMaxConnectionIdleTime() > 0) {
createConnectionReaper(client, connectionManager, asyncConnectionManager);
} else {
LOG.info("Idle connection reaping disabled");
}
// set discovery (should be set after setting the httpClient on jestClient)
if (httpClientConfig.isDiscoveryEnabled()) {
createNodeChecker(client, httpClientConfig);
} else {
LOG.info("Node Discovery disabled");
}
client.getAsyncClient().start();
return client;
}
/**
* Registers the given connection manager with this reaper;
*
* @param connectionManager the connection manager to be registered.
* @return true if the connection manager has been successfully registered; false otherwise.
*/
public static synchronized boolean registerConnectionManager(HttpClientConnectionManager connectionManager) {
if (instance == null) {
return false;
}
return connectionManagers.add(connectionManager);
}
@Bean
public CloseableHttpClient httpClient(ApacheHttpClientFactory httpClientFactory,
HttpClientConnectionManager httpClientConnectionManager,
FeignHttpClientProperties httpClientProperties) {
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setConnectTimeout(httpClientProperties.getConnectionTimeout())
.setRedirectsEnabled(httpClientProperties.isFollowRedirects())
.build();
this.httpClient = httpClientFactory.createBuilder()
.setConnectionManager(httpClientConnectionManager)
.setDefaultRequestConfig(defaultRequestConfig).build();
return this.httpClient;
}
@Test
public void disableSslTest() throws Exception {
HttpClientConnectionManager connectionManager = this.context
.getBean(HttpClientConnectionManager.class);
Lookup<ConnectionSocketFactory> socketFactoryRegistry = getConnectionSocketFactoryLookup(
connectionManager);
assertThat(socketFactoryRegistry.lookup("https")).isNotNull();
assertThat(this.getX509TrustManager(socketFactoryRegistry).getAcceptedIssuers())
.isNull();
}
@Override
public HttpClientConnectionManager newConnectionManager(
boolean disableSslValidation, int maxTotalConnections,
int maxConnectionsPerRoute, long timeToLive, TimeUnit timeUnit,
RegistryBuilder registry) {
return mock(PoolingHttpClientConnectionManager.class);
}
@Setup
public void setup() {
port = SocketUtils.findAvailableTcpPort();
System.setProperty("server.port", String.valueOf(port));
context = SpringApplication.run(HttpclientSpringMvcApplication.class);
ApacheHttpClientConnectionManagerFactory factory = new ApacheHttpClientConnectionManagerFactory();
HttpClientConnectionManager manager = factory.newConnectionManager(false, 100, 100);
ApacheHttpClientFactory apacheHttpClientFactory = new ApacheHttpClientFactory();
httpClient = apacheHttpClientFactory.createHttpClient(manager, new RaptorHttpClientProperties());
}
@Bean
@ConditionalOnMissingBean(HttpClient.class)
public CloseableHttpClient createHttpClient(ApacheHttpClientFactory httpClientFactory,
HttpClientConnectionManager httpClientConnectionManager) {
this.httpClient = httpClientFactory.createHttpClient(httpClientConnectionManager, httpClientProperties);
return this.httpClient;
}
public static CloseableHttpClient getHttpClient(HttpClientConnectionManager connectionManager, int maxConnections) {
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).build();
ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(StandardCharsets.UTF_8).build();
SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).setSoKeepAlive(true)
.setSoReuseAddress(true).build();
//setup http client
return HttpClients.custom().setConnectionManager(connectionManager)
.setUserAgent(getHttpUserAgent()).setRetryHandler(new MCRRetryHandler(maxConnections))
.setDefaultRequestConfig(requestConfig).setDefaultConnectionConfig(connectionConfig)
.setDefaultSocketConfig(socketConfig).build();
}
/**
* @param conMgr
* connection manager to use for this http client. <b>NOTE:</b>The
* provided {@link HttpClientConnectionManager} will not be
* {@link HttpClientConnectionManager#shutdown()} by this class.
* @param defaultConfig
* the default {@link RequestConfig} to set on the client. If
* {@code null} a default config is created w/ the default connection
* and socket timeouts.
*/
protected HttpClientBase(String host, int port, String path, HttpClientConnectionManager conMgr, RequestConfig defaultConfig) {
url = normalizedURL(host, port, path);
if (defaultConfig == null) {
this.defaultConfig = RequestConfig.custom()
.setConnectionRequestTimeout(DEFAULT_CONNECTION_TIMEOUT)
.setSocketTimeout(DEFAULT_SO_TIMEOUT).build();
} else {
this.defaultConfig = defaultConfig;
}
httpc = HttpClientBuilder.create().setConnectionManager(conMgr).setDefaultRequestConfig(this.defaultConfig).build();
}
public HttpClientConnectionManager createHttpClientConnectionManager() {
SSLContext sslContext = null;
try {
sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return false;
}
}).build();
} catch (Exception e) {
throw new RuntimeException(e);
}
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext,
NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory)
.build();
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(
socketFactoryRegistry);
// 最大连接数
poolingHttpClientConnectionManager.setMaxTotal(httpClientConfig.getMaxTotal());
// 单个站点最大连接数
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(httpClientConfig.getMaxPerRoute());
// 长连接
poolingHttpClientConnectionManager.setDefaultSocketConfig(
SocketConfig.custom().setSoTimeout(httpClientConfig.getSocketTimeout()).setSoKeepAlive(true).build());
// 连接不活跃多久检查毫秒 并不是100 % 可信
poolingHttpClientConnectionManager.setValidateAfterInactivity(httpClientConfig.getValidateAfterInactivity());
// 空闲扫描线程
HttpClientIdleConnectionMonitor.registerConnectionManager(poolingHttpClientConnectionManager, httpClientConfig);
return poolingHttpClientConnectionManager;
}
public static synchronized boolean registerConnectionManager(HttpClientConnectionManager connectionManager,
HttpClientConfig clientConfig) {
if (instance == null) {
instance = new HttpClientIdleConnectionMonitor(clientConfig);
instance.start();
}
return connectionManagers.add(connectionManager);
}
/**
* You should call {@link LogDataProvider#obtainUrl()} before
* */
@Override
public InputStream provide() {
HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
HttpClient httpClient = HttpClientBuilder.create()
.setConnectionManager(connectionManager)
.build();
String uri = url + "?s=" + userSessionSource.getUserSession().getId();
if (downloadFullLog) {
uri += "&full=true";
}
HttpGet httpGet = new HttpGet(uri);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
int httpStatus = httpResponse.getStatusLine().getStatusCode();
if (httpStatus == HttpStatus.SC_OK) {
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
inputStream = httpEntity.getContent();
} else {
log.debug("Unable to download log from " + url + "\nHttpEntity is null");
throw new RuntimeException("Unable to download log from " + url + "\nHttpEntity is null");
}
} else {
log.debug("Unable to download log from " + url + "\n" + httpResponse.getStatusLine());
throw new RuntimeException("Unable to download log from " + url + "\n" + httpResponse.getStatusLine());
}
} catch (IOException e) {
log.debug("Unable to download log from " + url + "\n" + e);
throw new RuntimeException(e);
}
return inputStream;
}
@Bean(name = "defaultHttpClientConnectionManager")
@ConditionalOnMissingBean
public HttpClientConnectionManager httpClientConnectionManager() {
PoolingHttpClientConnectionManager clientConnectionManager = new PoolingHttpClientConnectionManager();
clientConnectionManager.setMaxTotal(connectionManagerMaxTotal); // 整个连接池的并发
clientConnectionManager.setDefaultMaxPerRoute(connectionManagerDefaultMaxPerRoute); // 每个主机的并发
return clientConnectionManager;
}
private HttpClientConnectionManager getConnectionManager(Builder builder) {
if (builder.uri.getScheme().equals(NPIPE_SCHEME)) {
final BasicHttpClientConnectionManager bm =
new BasicHttpClientConnectionManager(getSchemeRegistry(builder));
return bm;
} else {
final PoolingHttpClientConnectionManager cm =
new PoolingHttpClientConnectionManager(getSchemeRegistry(builder));
// Use all available connections instead of artificially limiting ourselves to 2 per server.
cm.setMaxTotal(builder.connectionPoolSize);
cm.setDefaultMaxPerRoute(cm.getMaxTotal());
return cm;
}
}
public CloseableHttpClient buildPooledClient() throws IOException {
org.apache.http.impl.client.HttpClientBuilder builder = HttpClients.custom();
HttpClientConnectionManager manager = getPooledConnectionFactory(certPath, maxConnections);
builder.setConnectionManager(manager);
// TODO: For push-redirects working for 301, the redirect strategy should be relaxed (see #351)
// However not sure whether we should do it right now and whether this is correct, since normally
// a 301 should only occur when the image name is invalid (e.g. containing "//" in which case a redirect
// happens to the URL with a single "/")
// builder.setRedirectStrategy(new LaxRedirectStrategy());
// TODO: Tune client if needed (e.g. add pooling factoring .....
// But I think, that's not really required.
return builder.build();
}
private Driver createMockDriver(Properties properties, HttpClientConnectionManager connectionManager, String name) {
Driver driver =
Driver.builder()
.setName(name)
.setProperties(properties)
.setRequestExecutorBuilder(
HttpClientRequestExecutor.builder().setConnectionManager(connectionManager)).build();
DriverFactory.put(name, driver);
return driver;
}