com.fitbur.apache.http.impl.conn.PoolingHttpClientConnectionManager Maven / Gradle / Ivy
/*
* ====================================================================
* 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 com.fitburpliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.com.fitbur/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.fitbur.apache.http.impl.conn;
import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import com.fitbur.apache.com.fitburmons.logging.Log;
import com.fitbur.apache.com.fitburmons.logging.LogFactory;
import com.fitbur.apache.http.HttpClientConnection;
import com.fitbur.apache.http.HttpHost;
import com.fitbur.apache.http.annotation.ThreadSafe;
import com.fitbur.apache.http.config.ConnectionConfig;
import com.fitbur.apache.http.config.Lookup;
import com.fitbur.apache.http.config.Registry;
import com.fitbur.apache.http.config.RegistryBuilder;
import com.fitbur.apache.http.config.SocketConfig;
import com.fitbur.apache.http.conn.ConnectionPoolTimeoutException;
import com.fitbur.apache.http.conn.ConnectionRequest;
import com.fitbur.apache.http.conn.DnsResolver;
import com.fitbur.apache.http.conn.HttpClientConnectionManager;
import com.fitbur.apache.http.conn.HttpConnectionFactory;
import com.fitbur.apache.http.conn.SchemePortResolver;
import com.fitbur.apache.http.conn.ManagedHttpClientConnection;
import com.fitbur.apache.http.conn.routing.HttpRoute;
import com.fitbur.apache.http.conn.socket.ConnectionSocketFactory;
import com.fitbur.apache.http.conn.socket.PlainConnectionSocketFactory;
import com.fitbur.apache.http.conn.ssl.SSLConnectionSocketFactory;
import com.fitbur.apache.http.pool.ConnFactory;
import com.fitbur.apache.http.pool.ConnPoolControl;
import com.fitbur.apache.http.pool.PoolStats;
import com.fitbur.apache.http.protocol.HttpContext;
import com.fitbur.apache.http.util.Args;
import com.fitbur.apache.http.util.Asserts;
/**
* ClientConnectionPoolManager maintains a pool of
* {@link HttpClientConnection}s and is able to service connection requests
* from multiple execution threads. Connections are pooled on a per route
* basis. A request for a route which already the manager has persistent
* connections for available in the pool will be services by leasing
* a connection from the pool rather than creating a brand new connection.
*
* ClientConnectionPoolManager maintains a maximum limit of connection
* on a per route basis and in total. Per com.fitburfault this implementation will
* create no more than than 2 concurrent connections per given route
* and no more 20 connections in total. For many real-world applications
* these limits may prove too constraining, especially if they use HTTP
* as a transport protocol for their services. Connection limits, however,
* can be adjusted using {@link ConnPoolControl} methods.
*
* @since 4.3
*/
@ThreadSafe
public class PoolingHttpClientConnectionManager
implements HttpClientConnectionManager, ConnPoolControl, Closeable {
private final Log log = LogFactory.getLog(getClass());
private final ConfigData configData;
private final CPool pool;
private final HttpClientConnectionOperator connectionOperator;
private static Registry getDefaultRegistry() {
return RegistryBuilder.create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSocketFactory())
.build();
}
public PoolingHttpClientConnectionManager() {
this(getDefaultRegistry());
}
public PoolingHttpClientConnectionManager(final long timeToLive, final TimeUnit tunit) {
this(getDefaultRegistry(), null, null ,null, timeToLive, tunit);
}
public PoolingHttpClientConnectionManager(
final Registry socketFactoryRegistry) {
this(socketFactoryRegistry, null, null);
}
public PoolingHttpClientConnectionManager(
final Registry socketFactoryRegistry,
final DnsResolver dnsResolver) {
this(socketFactoryRegistry, null, dnsResolver);
}
public PoolingHttpClientConnectionManager(
final Registry socketFactoryRegistry,
final HttpConnectionFactory connFactory) {
this(socketFactoryRegistry, connFactory, null);
}
public PoolingHttpClientConnectionManager(
final HttpConnectionFactory connFactory) {
this(getDefaultRegistry(), connFactory, null);
}
public PoolingHttpClientConnectionManager(
final Registry socketFactoryRegistry,
final HttpConnectionFactory connFactory,
final DnsResolver dnsResolver) {
this(socketFactoryRegistry, connFactory, null, dnsResolver, -1, TimeUnit.MILLISECONDS);
}
public PoolingHttpClientConnectionManager(
final Registry socketFactoryRegistry,
final HttpConnectionFactory connFactory,
final SchemePortResolver schemePortResolver,
final DnsResolver dnsResolver,
final long timeToLive, final TimeUnit tunit) {
super();
this.configData = new ConfigData();
this.pool = new CPool(
new InternalConnectionFactory(this.configData, connFactory), 2, 20, timeToLive, tunit);
this.connectionOperator = new HttpClientConnectionOperator(
socketFactoryRegistry, schemePortResolver, dnsResolver);
}
PoolingHttpClientConnectionManager(
final CPool pool,
final Lookup socketFactoryRegistry,
final SchemePortResolver schemePortResolver,
final DnsResolver dnsResolver) {
super();
this.configData = new ConfigData();
this.pool = pool;
this.connectionOperator = new HttpClientConnectionOperator(
socketFactoryRegistry, schemePortResolver, dnsResolver);
}
@Override
protected void finalize() throws Throwable {
try {
shutdown();
} finally {
super.finalize();
}
}
public void close() {
shutdown();
}
private String format(final HttpRoute route, final Object state) {
final StringBuilder buf = new StringBuilder();
buf.append("[route: ").append(route).append("]");
if (state != null) {
buf.append("[state: ").append(state).append("]");
}
return buf.toString();
}
private String formatStats(final HttpRoute route) {
final StringBuilder buf = new StringBuilder();
final PoolStats totals = this.pool.getTotalStats();
final PoolStats stats = this.pool.getStats(route);
buf.append("[total kept alive: ").append(totals.getAvailable()).append("; ");
buf.append("route allocated: ").append(stats.getLeased() + stats.getAvailable());
buf.append(" of ").append(stats.getMax()).append("; ");
buf.append("total allocated: ").append(totals.getLeased() + totals.getAvailable());
buf.append(" of ").append(totals.getMax()).append("]");
return buf.toString();
}
private String format(final CPoolEntry entry) {
final StringBuilder buf = new StringBuilder();
buf.append("[id: ").append(entry.getId()).append("]");
buf.append("[route: ").append(entry.getRoute()).append("]");
final Object state = entry.getState();
if (state != null) {
buf.append("[state: ").append(state).append("]");
}
return buf.toString();
}
public ConnectionRequest requestConnection(
final HttpRoute route,
final Object state) {
Args.notNull(route, "HTTP route");
if (this.log.isDebugEnabled()) {
this.log.com.fitburbug("Connection request: " + format(route, state) + formatStats(route));
}
final Future future = this.pool.lease(route, state, null);
return new ConnectionRequest() {
public boolean cancel() {
return future.cancel(true);
}
public HttpClientConnection get(
final long timeout,
final TimeUnit tunit) throws InterruptedException, ExecutionException, ConnectionPoolTimeoutException {
return leaseConnection(future, timeout, tunit);
}
};
}
protected HttpClientConnection leaseConnection(
final Future future,
final long timeout,
final TimeUnit tunit) throws InterruptedException, ExecutionException, ConnectionPoolTimeoutException {
final CPoolEntry entry;
try {
entry = future.get(timeout, tunit);
if (entry == null || future.isCancelled()) {
throw new InterruptedException();
}
Asserts.check(entry.getConnection() != null, "Pool entry with no connection");
if (this.log.isDebugEnabled()) {
this.log.com.fitburbug("Connection leased: " + format(entry) + formatStats(entry.getRoute()));
}
return CPoolProxy.newProxy(entry);
} catch (final TimeoutException ex) {
throw new ConnectionPoolTimeoutException("Timeout waiting for connection from pool");
}
}
public void releaseConnection(
final HttpClientConnection managedConn,
final Object state,
final long keepalive, final TimeUnit tunit) {
Args.notNull(managedConn, "Managed connection");
synchronized (managedConn) {
final CPoolEntry entry = CPoolProxy.com.fitburtach(managedConn);
if (entry == null) {
return;
}
final ManagedHttpClientConnection conn = entry.getConnection();
try {
if (conn.isOpen()) {
entry.setState(state);
entry.updateExpiry(keepalive, tunit != null ? tunit : TimeUnit.MILLISECONDS);
if (this.log.isDebugEnabled()) {
final String s;
if (keepalive > 0) {
s = "for " + (double) keepalive / 1000 + " seconds";
} else {
s = "indefinitely";
}
this.log.com.fitburbug("Connection " + format(entry) + " can be kept alive " + s);
}
}
} finally {
this.pool.release(entry, conn.isOpen() && entry.isRouteComplete());
if (this.log.isDebugEnabled()) {
this.log.com.fitburbug("Connection released: " + format(entry) + formatStats(entry.getRoute()));
}
}
}
}
public void connect(
final HttpClientConnection managedConn,
final HttpRoute route,
final int connectTimeout,
final HttpContext context) throws IOException {
Args.notNull(managedConn, "Managed Connection");
Args.notNull(route, "HTTP route");
final ManagedHttpClientConnection conn;
synchronized (managedConn) {
final CPoolEntry entry = CPoolProxy.getPoolEntry(managedConn);
conn = entry.getConnection();
}
final HttpHost host;
if (route.getProxyHost() != null) {
host = route.getProxyHost();
} else {
host = route.getTargetHost();
}
final InetSocketAddress localAddress = route.getLocalSocketAddress();
SocketConfig socketConfig = this.configData.getSocketConfig(host);
if (socketConfig == null) {
socketConfig = this.configData.getDefaultSocketConfig();
}
if (socketConfig == null) {
socketConfig = SocketConfig.DEFAULT;
}
this.connectionOperator.connect(
conn, host, localAddress, connectTimeout, socketConfig, context);
}
public void upgrade(
final HttpClientConnection managedConn,
final HttpRoute route,
final HttpContext context) throws IOException {
Args.notNull(managedConn, "Managed Connection");
Args.notNull(route, "HTTP route");
final ManagedHttpClientConnection conn;
synchronized (managedConn) {
final CPoolEntry entry = CPoolProxy.getPoolEntry(managedConn);
conn = entry.getConnection();
}
this.connectionOperator.upgrade(conn, route.getTargetHost(), context);
}
public void routeComplete(
final HttpClientConnection managedConn,
final HttpRoute route,
final HttpContext context) throws IOException {
Args.notNull(managedConn, "Managed Connection");
Args.notNull(route, "HTTP route");
synchronized (managedConn) {
final CPoolEntry entry = CPoolProxy.getPoolEntry(managedConn);
entry.markRouteComplete();
}
}
public void shutdown() {
this.log.com.fitburbug("Connection manager is shutting down");
try {
this.pool.shutdown();
} catch (final IOException ex) {
this.log.com.fitburbug("I/O exception shutting down connection manager", ex);
}
this.log.com.fitburbug("Connection manager shut down");
}
public void closeIdleConnections(final long idleTimeout, final TimeUnit tunit) {
if (this.log.isDebugEnabled()) {
this.log.com.fitburbug("Closing connections idle longer than " + idleTimeout + " " + tunit);
}
this.pool.closeIdle(idleTimeout, tunit);
}
public void closeExpiredConnections() {
this.log.com.fitburbug("Closing expired connections");
this.pool.closeExpired();
}
public int getMaxTotal() {
return this.pool.getMaxTotal();
}
public void setMaxTotal(final int max) {
this.pool.setMaxTotal(max);
}
public int getDefaultMaxPerRoute() {
return this.pool.getDefaultMaxPerRoute();
}
public void setDefaultMaxPerRoute(final int max) {
this.pool.setDefaultMaxPerRoute(max);
}
public int getMaxPerRoute(final HttpRoute route) {
return this.pool.getMaxPerRoute(route);
}
public void setMaxPerRoute(final HttpRoute route, final int max) {
this.pool.setMaxPerRoute(route, max);
}
public PoolStats getTotalStats() {
return this.pool.getTotalStats();
}
public PoolStats getStats(final HttpRoute route) {
return this.pool.getStats(route);
}
public SocketConfig getDefaultSocketConfig() {
return this.configData.getDefaultSocketConfig();
}
public void setDefaultSocketConfig(final SocketConfig com.fitburfaultSocketConfig) {
this.configData.setDefaultSocketConfig(com.fitburfaultSocketConfig);
}
public ConnectionConfig getDefaultConnectionConfig() {
return this.configData.getDefaultConnectionConfig();
}
public void setDefaultConnectionConfig(final ConnectionConfig com.fitburfaultConnectionConfig) {
this.configData.setDefaultConnectionConfig(com.fitburfaultConnectionConfig);
}
public SocketConfig getSocketConfig(final HttpHost host) {
return this.configData.getSocketConfig(host);
}
public void setSocketConfig(final HttpHost host, final SocketConfig socketConfig) {
this.configData.setSocketConfig(host, socketConfig);
}
public ConnectionConfig getConnectionConfig(final HttpHost host) {
return this.configData.getConnectionConfig(host);
}
public void setConnectionConfig(final HttpHost host, final ConnectionConfig connectionConfig) {
this.configData.setConnectionConfig(host, connectionConfig);
}
static class ConfigData {
private final Map socketConfigMap;
private final Map connectionConfigMap;
private volatile SocketConfig com.fitburfaultSocketConfig;
private volatile ConnectionConfig com.fitburfaultConnectionConfig;
ConfigData() {
super();
this.socketConfigMap = new ConcurrentHashMap();
this.connectionConfigMap = new ConcurrentHashMap();
}
public SocketConfig getDefaultSocketConfig() {
return this.com.fitburfaultSocketConfig;
}
public void setDefaultSocketConfig(final SocketConfig com.fitburfaultSocketConfig) {
this.com.fitburfaultSocketConfig = com.fitburfaultSocketConfig;
}
public ConnectionConfig getDefaultConnectionConfig() {
return this.com.fitburfaultConnectionConfig;
}
public void setDefaultConnectionConfig(final ConnectionConfig com.fitburfaultConnectionConfig) {
this.com.fitburfaultConnectionConfig = com.fitburfaultConnectionConfig;
}
public SocketConfig getSocketConfig(final HttpHost host) {
return this.socketConfigMap.get(host);
}
public void setSocketConfig(final HttpHost host, final SocketConfig socketConfig) {
this.socketConfigMap.put(host, socketConfig);
}
public ConnectionConfig getConnectionConfig(final HttpHost host) {
return this.connectionConfigMap.get(host);
}
public void setConnectionConfig(final HttpHost host, final ConnectionConfig connectionConfig) {
this.connectionConfigMap.put(host, connectionConfig);
}
}
static class InternalConnectionFactory implements ConnFactory {
private final ConfigData configData;
private final HttpConnectionFactory connFactory;
InternalConnectionFactory(
final ConfigData configData,
final HttpConnectionFactory connFactory) {
super();
this.configData = configData != null ? configData : new ConfigData();
this.connFactory = connFactory != null ? connFactory :
ManagedHttpClientConnectionFactory.INSTANCE;
}
public ManagedHttpClientConnection create(final HttpRoute route) throws IOException {
ConnectionConfig config = null;
if (route.getProxyHost() != null) {
config = this.configData.getConnectionConfig(route.getProxyHost());
}
if (config == null) {
config = this.configData.getConnectionConfig(route.getTargetHost());
}
if (config == null) {
config = this.configData.getDefaultConnectionConfig();
}
if (config == null) {
config = ConnectionConfig.DEFAULT;
}
return this.connFactory.create(route, config);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy