Simplified configuration of connection managers. Total connection maximum and maximum connection per route limits can be set using methods of the class instead of HTTP parameters

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@820971 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2009-10-02 11:33:00 +00:00
parent 91662f97ad
commit 30b38eb6e4
25 changed files with 387 additions and 398 deletions

View File

@ -1,6 +1,11 @@
Changes since 4.0
-------------------
* Simplified configuration of connection managers. Total connection maximum
and maximum connection per route limits can be set using methods of
the class instead of HTTP parameters.
Contributed by Oleg Kalnichevski <olegk at apache.org>
* Added parameters to define the order of preference for supported auth
schemes for target host and proxy authentication.
Contributed by Oleg Kalnichevski <olegk at apache.org>

View File

@ -30,19 +30,14 @@ import java.util.concurrent.TimeUnit;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
/**
* Example demonstrating how to evict expired and idle connections
@ -51,18 +46,15 @@ import org.apache.http.params.HttpProtocolParams;
public class ClientEvictExpiredConnections {
public static void main(String[] args) throws Exception {
// Create and initialize HTTP parameters
HttpParams params = new BasicHttpParams();
ConnManagerParams.setMaxTotalConnections(params, 100);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
// Create and initialize scheme registry
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(
new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
HttpClient httpclient = new DefaultHttpClient(cm, params);
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
cm.setMaxTotalConnections(100);
HttpClient httpclient = new DefaultHttpClient(cm);
// create an array of URIs to perform GETs on
String[] urisToGet = {

View File

@ -28,21 +28,15 @@ package org.apache.http.examples.client;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
/**
@ -52,11 +46,6 @@ import org.apache.http.util.EntityUtils;
public class ClientMultiThreadedExecution {
public static void main(String[] args) throws Exception {
// Create and initialize HTTP parameters
HttpParams params = new BasicHttpParams();
ConnManagerParams.setMaxTotalConnections(params, 100);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
// Create and initialize scheme registry
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(
@ -65,8 +54,10 @@ public class ClientMultiThreadedExecution {
// Create an HttpClient with the ThreadSafeClientConnManager.
// This connection manager must be used if more than one thread will
// be using the HttpClient.
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
HttpClient httpClient = new DefaultHttpClient(cm, params);
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
cm.setMaxTotalConnections(100);
HttpClient httpClient = new DefaultHttpClient(cm);
// create an array of URIs to perform GETs on
String[] urisToGet = {

View File

@ -145,7 +145,7 @@ public class ManagerConnectDirect {
private final static ClientConnectionManager createManager() {
return new ThreadSafeClientConnManager(getParams(), supportedSchemes);
return new ThreadSafeClientConnManager(supportedSchemes);
}

View File

@ -173,7 +173,7 @@ public class ManagerConnectProxy {
private final static ClientConnectionManager createManager() {
return new ThreadSafeClientConnManager(getParams(), supportedSchemes);
return new ThreadSafeClientConnManager(supportedSchemes);
}

View File

@ -51,6 +51,7 @@ public interface ConnManagerPNames {
* This parameter expects a value of type {@link ConnPerRoute}.
* </p>
*/
@Deprecated
public static final String MAX_CONNECTIONS_PER_ROUTE = "http.conn-manager.max-per-route";
/**
@ -61,6 +62,7 @@ public interface ConnManagerPNames {
* This parameter expects a value of type {@link Integer}.
* </p>
*/
@Deprecated
public static final String MAX_TOTAL_CONNECTIONS = "http.conn-manager.max-total";
}

View File

@ -51,11 +51,13 @@ public class ConnManagerParamBean extends HttpAbstractParamBean {
}
/** @see ConnManagerPNames#MAX_TOTAL_CONNECTIONS */
@Deprecated
public void setMaxTotalConnections (final int maxConnections) {
params.setIntParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, maxConnections);
}
/** @see ConnManagerPNames#MAX_CONNECTIONS_PER_ROUTE */
@Deprecated
public void setConnectionsPerRoute(final ConnPerRouteBean connPerRoute) {
params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, connPerRoute);
}

View File

@ -74,6 +74,7 @@ public final class ConnManagerParams implements ConnManagerPNames {
}
/** The default maximum number of connections allowed per host */
@Deprecated
private static final ConnPerRoute DEFAULT_CONN_PER_ROUTE = new ConnPerRoute() {
public int getMaxForRoute(HttpRoute route) {
@ -89,8 +90,11 @@ public final class ConnManagerParams implements ConnManagerPNames {
* @param connPerRoute lookup interface for maximum number of connections allowed
* per route
*
* @deprecated do not use
*
* @see ConnManagerPNames#MAX_CONNECTIONS_PER_ROUTE
*/
@Deprecated
public static void setMaxConnectionsPerRoute(final HttpParams params,
final ConnPerRoute connPerRoute) {
if (params == null) {
@ -107,8 +111,11 @@ public final class ConnManagerParams implements ConnManagerPNames {
*
* @return lookup interface for maximum number of connections allowed per route.
*
* @deprecated do not use
*
* @see ConnManagerPNames#MAX_CONNECTIONS_PER_ROUTE
*/
@Deprecated
public static ConnPerRoute getMaxConnectionsPerRoute(final HttpParams params) {
if (params == null) {
throw new IllegalArgumentException
@ -128,8 +135,11 @@ public final class ConnManagerParams implements ConnManagerPNames {
* @param params HTTP parameters
* @param maxTotalConnections The maximum number of connections allowed.
*
* @deprecated do not use
*
* @see ConnManagerPNames#MAX_TOTAL_CONNECTIONS
*/
@Deprecated
public static void setMaxTotalConnections(
final HttpParams params,
int maxTotalConnections) {
@ -147,8 +157,11 @@ public final class ConnManagerParams implements ConnManagerPNames {
*
* @return The maximum number of connections allowed.
*
* @deprecated do not use
*
* @see ConnManagerPNames#MAX_TOTAL_CONNECTIONS
*/
@Deprecated
public static int getMaxTotalConnections(
final HttpParams params) {
if (params == null) {

View File

@ -29,7 +29,8 @@ package org.apache.http.conn.params;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.annotation.GuardedBy;
import org.apache.http.annotation.ThreadSafe;
import org.apache.http.conn.routing.HttpRoute;
@ -41,14 +42,16 @@ import org.apache.http.conn.routing.HttpRoute;
*
* @since 4.0
*/
@NotThreadSafe // maxPerHostMap and defaultMax
@ThreadSafe
public final class ConnPerRouteBean implements ConnPerRoute {
/** The default maximum number of connections allowed per host */
public static final int DEFAULT_MAX_CONNECTIONS_PER_ROUTE = 2; // Per RFC 2616 sec 8.1.4
@GuardedBy("this")
private final Map<HttpRoute, Integer> maxPerHostMap;
@GuardedBy("this")
private int defaultMax;
public ConnPerRouteBean(int defaultMax) {
@ -61,11 +64,19 @@ public final class ConnPerRouteBean implements ConnPerRoute {
this(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
}
public int getDefaultMax() {
@Deprecated
public synchronized int getDefaultMax() {
return this.defaultMax;
}
public void setDefaultMaxPerRoute(int max) {
/**
* @since 4.1
*/
public synchronized int getDefaultMaxPerRoute() {
return this.defaultMax;
}
public synchronized void setDefaultMaxPerRoute(int max) {
if (max < 1) {
throw new IllegalArgumentException
("The maximum must be greater than 0.");
@ -73,7 +84,7 @@ public final class ConnPerRouteBean implements ConnPerRoute {
this.defaultMax = max;
}
public void setMaxForRoute(final HttpRoute route, int max) {
public synchronized void setMaxForRoute(final HttpRoute route, int max) {
if (route == null) {
throw new IllegalArgumentException
("HTTP route may not be null.");
@ -85,7 +96,7 @@ public final class ConnPerRouteBean implements ConnPerRoute {
this.maxPerHostMap.put(route, Integer.valueOf(max));
}
public int getMaxForRoute(final HttpRoute route) {
public synchronized int getMaxForRoute(final HttpRoute route) {
if (route == null) {
throw new IllegalArgumentException
("HTTP route may not be null.");
@ -98,7 +109,7 @@ public final class ConnPerRouteBean implements ConnPerRoute {
}
}
public void setMaxForRoutes(final Map<HttpRoute, Integer> map) {
public synchronized void setMaxForRoutes(final Map<HttpRoute, Integer> map) {
if (map == null) {
return;
}
@ -106,9 +117,9 @@ public final class ConnPerRouteBean implements ConnPerRoute {
this.maxPerHostMap.putAll(map);
}
@Override
public String toString() {
return this.maxPerHostMap.toString();
}
@Override
public synchronized String toString() {
return this.maxPerHostMap.toString();
}
}

View File

@ -134,8 +134,6 @@ import org.apache.http.util.VersionInfo;
* <li>{@link org.apache.http.conn.params.ConnRoutePNames#LOCAL_ADDRESS}</li>
* <li>{@link org.apache.http.conn.params.ConnRoutePNames#DEFAULT_PROXY}</li>
* <li>{@link org.apache.http.conn.params.ConnManagerPNames#TIMEOUT}</li>
* <li>{@link org.apache.http.conn.params.ConnManagerPNames#MAX_CONNECTIONS_PER_ROUTE}</li>
* <li>{@link org.apache.http.conn.params.ConnManagerPNames#MAX_TOTAL_CONNECTIONS}</li>
* <li>{@link org.apache.http.cookie.params.CookieSpecPNames#DATE_PATTERNS}</li>
* <li>{@link org.apache.http.cookie.params.CookieSpecPNames#SINGLE_COOKIE_HEADER}</li>
* <li>{@link org.apache.http.auth.params.AuthPNames#CREDENTIAL_CHARSET}</li>
@ -155,7 +153,6 @@ import org.apache.http.util.VersionInfo;
@ThreadSafe
public class DefaultHttpClient extends AbstractHttpClient {
/**
* Creates a new HTTP client from parameters and a connection manager.
*
@ -168,6 +165,15 @@ public class DefaultHttpClient extends AbstractHttpClient {
super(conman, params);
}
/**
* @since 4.1
*/
public DefaultHttpClient(
final ClientConnectionManager conman) {
super(conman, null);
}
public DefaultHttpClient(final HttpParams params) {
super(null, params);
@ -241,7 +247,7 @@ public class DefaultHttpClient extends AbstractHttpClient {
if (factory != null) {
connManager = factory.newInstance(params, registry);
} else {
connManager = new SingleClientConnManager(getParams(), registry);
connManager = new SingleClientConnManager(registry);
}
return connManager;

View File

@ -102,9 +102,21 @@ public class SingleClientConnManager implements ClientConnectionManager {
* @param params the parameters for this manager
* @param schreg the scheme registry, or
* <code>null</code> for the default registry
*
* @deprecated use {@link SingleClientConnManager#SingleClientConnManager(SchemeRegistry)}
*/
@Deprecated
public SingleClientConnManager(HttpParams params,
SchemeRegistry schreg) {
this(schreg);
}
/**
* Creates a new simple connection manager.
*
* @param schreg the scheme registry, or
* <code>null</code> for the default registry
*/
public SingleClientConnManager(final SchemeRegistry schreg) {
if (schreg == null) {
throw new IllegalArgumentException
("Scheme registry must not be null.");
@ -236,8 +248,8 @@ public class SingleClientConnManager implements ClientConnectionManager {
}
public synchronized void releaseConnection(
ManagedClientConnection conn,
long validDuration, TimeUnit timeUnit) {
ManagedClientConnection conn,
long validDuration, TimeUnit timeUnit) {
assertStillUp();
if (!(conn instanceof ConnAdapter)) {

View File

@ -60,7 +60,7 @@ import org.apache.http.impl.conn.IdleConnectionHandler;
@SuppressWarnings("deprecation")
public abstract class AbstractConnPool implements RefQueueHandler {
private final Log log = LogFactory.getLog(getClass());
private final Log log;
/**
* The global lock for this pool.
@ -95,11 +95,11 @@ public abstract class AbstractConnPool implements RefQueueHandler {
* Creates a new connection pool.
*/
protected AbstractConnPool() {
leasedConnections = new HashSet<BasicPoolEntry>();
idleConnHandler = new IdleConnectionHandler();
boolean fair = false; //@@@ check parameters to decide
poolLock = new ReentrantLock(fair);
super();
this.log = LogFactory.getLog(getClass());
this.leasedConnections = new HashSet<BasicPoolEntry>();
this.idleConnHandler = new IdleConnectionHandler();
this.poolLock = new ReentrantLock();
}
/**

View File

@ -37,6 +37,8 @@ import java.util.concurrent.TimeUnit;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.annotation.GuardedBy;
import org.apache.http.annotation.ThreadSafe;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.ClientConnectionOperator;
import org.apache.http.conn.ConnectionPoolTimeoutException;
@ -60,14 +62,16 @@ import org.apache.http.params.HttpParams;
*
* @since 4.0
*/
@ThreadSafe
public class ConnPoolByRoute extends AbstractConnPool {
private final Log log = LogFactory.getLog(getClass());
private final HttpParams params;
/** Connection operator for this pool */
protected final ClientConnectionOperator operator;
/** Connections per route lookup */
protected final ConnPerRoute connPerRoute;
/** The list of free connections */
protected final Queue<BasicPoolEntry> freeConnections;
@ -80,24 +84,47 @@ public class ConnPoolByRoute extends AbstractConnPool {
* Keys are of class {@link HttpRoute},
* values of class {@link RouteSpecificPool}.
*/
@GuardedBy("poolLock")
protected final Map<HttpRoute, RouteSpecificPool> routeToPool;
@GuardedBy("poolLock")
protected volatile int maxTotalConnections;
/**
* Creates a new connection pool, managed by route.
*
* @since 4.1
*/
public ConnPoolByRoute(final ClientConnectionOperator operator, final HttpParams params) {
public ConnPoolByRoute(
final ClientConnectionOperator operator,
final ConnPerRoute connPerRoute,
int maxTotalConnections) {
super();
if (operator == null) {
throw new IllegalArgumentException("Connection operator may not be null");
}
if (connPerRoute == null) {
throw new IllegalArgumentException("Connections per route may not be null");
}
this.operator = operator;
this.params = params;
freeConnections = createFreeConnQueue();
waitingThreads = createWaitingThreadQueue();
routeToPool = createRouteToPoolMap();
this.connPerRoute = connPerRoute;
this.maxTotalConnections = maxTotalConnections;
this.freeConnections = createFreeConnQueue();
this.waitingThreads = createWaitingThreadQueue();
this.routeToPool = createRouteToPoolMap();
}
/**
* Creates a new connection pool, managed by route.
*
* @deprecated use {@link ConnPoolByRoute#ConnPoolByRoute(ClientConnectionOperator, ConnPerRoute)}
*/
@Deprecated
public ConnPoolByRoute(final ClientConnectionOperator operator, final HttpParams params) {
this(operator,
ConnManagerParams.getMaxConnectionsPerRoute(params),
ConnManagerParams.getMaxTotalConnections(params));
}
/**
* Creates the queue for {@link #freeConnections}.
@ -139,8 +166,7 @@ public class ConnPoolByRoute extends AbstractConnPool {
* @return the new pool
*/
protected RouteSpecificPool newRouteSpecificPool(HttpRoute route) {
ConnPerRoute connPerRoute = ConnManagerParams.getMaxConnectionsPerRoute(params);
return new RouteSpecificPool(route, connPerRoute.getMaxForRoute(route));
return new RouteSpecificPool(route, this.connPerRoute.getMaxForRoute(route));
}
@ -255,8 +281,6 @@ public class ConnPoolByRoute extends AbstractConnPool {
WaitingThreadAborter aborter)
throws ConnectionPoolTimeoutException, InterruptedException {
int maxTotalConnections = ConnManagerParams.getMaxTotalConnections(params);
Date deadline = null;
if (timeout > 0) {
deadline = new Date
@ -685,6 +709,26 @@ public class ConnPoolByRoute extends AbstractConnPool {
}
}
/**
* since 4.1
*/
public void setMaxTotalConnections(int max) {
poolLock.lock();
try {
maxTotalConnections = max;
} finally {
poolLock.unlock();
}
}
/**
* since 4.1
*/
public int getMaxTotalConnections() {
return maxTotalConnections;
}
} // class ConnPoolByRoute

View File

@ -31,6 +31,8 @@ import java.util.concurrent.TimeUnit;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.annotation.ThreadSafe;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ClientConnectionManager;
@ -57,53 +59,69 @@ import org.apache.http.impl.conn.DefaultClientConnectionOperator;
* 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 HTTP parameters.
* <p>
* The following parameters can be used to customize the behavior of this
* class:
* <ul>
* <li>{@link org.apache.http.conn.params.ConnManagerPNames#MAX_TOTAL_CONNECTIONS}</li>
* <li>{@link org.apache.http.conn.params.ConnManagerPNames#MAX_CONNECTIONS_PER_ROUTE}</li>
* </ul>
*
* @see org.apache.http.conn.params.ConnPerRoute
* @see org.apache.http.conn.params.ConnPerRouteBean
*
* @since 4.0
*/
@ThreadSafe
public class ThreadSafeClientConnManager implements ClientConnectionManager {
private final Log log = LogFactory.getLog(getClass());
private final Log log;
/** The schemes supported by this connection manager. */
protected final SchemeRegistry schemeRegistry; // @ThreadSafe
/** The pool of connections being managed. */
@Deprecated
protected final AbstractConnPool connectionPool;
/** The pool of connections being managed. */
protected final ConnPoolByRoute pool;
/** The operator for opening and updating connections. */
protected final ClientConnectionOperator connOperator; // DefaultClientConnectionOperator is @ThreadSafe
protected final ConnPerRouteBean connPerRoute;
/**
* Creates a new thread safe connection manager.
*
* @param params the parameters for this manager.
* @param schreg the scheme registry.
*/
public ThreadSafeClientConnManager(HttpParams params,
SchemeRegistry schreg) {
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}
public ThreadSafeClientConnManager(final SchemeRegistry schreg) {
super();
if (schreg == null) {
throw new IllegalArgumentException("Scheme registry may not be null");
}
this.log = LogFactory.getLog(getClass());
this.schemeRegistry = schreg;
this.connOperator = createConnectionOperator(schreg);
this.connectionPool = createConnectionPool(params);
this.connPerRoute = new ConnPerRouteBean();
this.connOperator = createConnectionOperator(schreg);
this.pool = createConnectionPool() ;
this.connectionPool = this.pool;
}
/**
* Creates a new thread safe connection manager.
*
* @param params the parameters for this manager.
* @param schreg the scheme registry.
*
* @deprecated use {@link ThreadSafeClientConnManager#ThreadSafeClientConnManager(SchemeRegistry)}
*/
@Deprecated
public ThreadSafeClientConnManager(HttpParams params,
SchemeRegistry schreg) {
if (schreg == null) {
throw new IllegalArgumentException("Scheme registry may not be null");
}
this.log = LogFactory.getLog(getClass());
this.schemeRegistry = schreg;
this.connPerRoute = new ConnPerRouteBean();
this.connOperator = createConnectionOperator(schreg);
this.pool = (ConnPoolByRoute) createConnectionPool(params) ;
this.connectionPool = this.pool;
}
@Override
protected void finalize() throws Throwable {
try {
@ -117,11 +135,24 @@ public class ThreadSafeClientConnManager implements ClientConnectionManager {
* Hook for creating the connection pool.
*
* @return the connection pool to use
*
* @deprecated use {@link #createConnectionPool(ConnPerRouteBean)}
*/
protected AbstractConnPool createConnectionPool(final HttpParams params) {
return new ConnPoolByRoute(connOperator, params);
}
/**
* Hook for creating the connection pool.
*
* @return the connection pool to use
*
* @since 4.1
*/
protected ConnPoolByRoute createConnectionPool() {
return new ConnPoolByRoute(connOperator, connPerRoute, 20);
}
/**
* Hook for creating the connection operator.
* It is called by the constructor.
@ -148,7 +179,7 @@ public class ThreadSafeClientConnManager implements ClientConnectionManager {
final HttpRoute route,
final Object state) {
final PoolEntryRequest poolRequest = connectionPool.requestPoolEntry(
final PoolEntryRequest poolRequest = pool.requestPoolEntry(
route, state);
return new ClientConnectionRequest() {
@ -220,14 +251,14 @@ public class ThreadSafeClientConnManager implements ClientConnectionManager {
}
hca.detach();
if (entry != null) {
connectionPool.freeEntry(entry, reusable, validDuration, timeUnit);
pool.freeEntry(entry, reusable, validDuration, timeUnit);
}
}
}
public void shutdown() {
log.debug("Shutting down");
connectionPool.shutdown();
pool.shutdown();
}
/**
@ -240,9 +271,8 @@ public class ThreadSafeClientConnManager implements ClientConnectionManager {
*
* @return the total number of pooled connections for that route
*/
public int getConnectionsInPool(HttpRoute route) {
return ((ConnPoolByRoute)connectionPool).getConnectionsInPool(
route);
public int getConnectionsInPool(final HttpRoute route) {
return pool.getConnectionsInPool(route);
}
/**
@ -254,25 +284,68 @@ public class ThreadSafeClientConnManager implements ClientConnectionManager {
* @return the total number of pooled connections
*/
public int getConnectionsInPool() {
int count;
connectionPool.poolLock.lock();
count = connectionPool.numConnections; //@@@
connectionPool.poolLock.unlock();
return count;
pool.poolLock.lock();
try {
return pool.numConnections;
} finally {
pool.poolLock.unlock();
}
}
public void closeIdleConnections(long idleTimeout, TimeUnit tunit) {
if (log.isDebugEnabled()) {
log.debug("Closing connections idle for " + idleTimeout + " " + tunit);
}
connectionPool.closeIdleConnections(idleTimeout, tunit);
connectionPool.deleteClosedConnections();
pool.closeIdleConnections(idleTimeout, tunit);
pool.deleteClosedConnections();
}
public void closeExpiredConnections() {
log.debug("Closing expired connections");
connectionPool.closeExpiredConnections();
connectionPool.deleteClosedConnections();
pool.closeExpiredConnections();
pool.deleteClosedConnections();
}
/**
* since 4.1
*/
public int getMaxTotalConnections() {
return pool.getMaxTotalConnections();
}
/**
* since 4.1
*/
public void setMaxTotalConnections(int max) {
pool.setMaxTotalConnections(max);
}
/**
* @since 4.1
*/
public int getDefaultMaxPerRoute() {
return connPerRoute.getDefaultMaxPerRoute();
}
/**
* @since 4.1
*/
public void setDefaultMaxPerRoute(int max) {
connPerRoute.setDefaultMaxPerRoute(max);
}
/**
* @since 4.1
*/
public int getMaxForRoute(final HttpRoute route) {
return connPerRoute.getMaxForRoute(route);
}
/**
* @since 4.1
*/
public void setMaxForRoute(final HttpRoute route, int max) {
connPerRoute.setMaxForRoute(route, max);
}
}

View File

@ -42,8 +42,6 @@ import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.MalformedChunkCodingException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.BasicHttpEntity;
@ -51,7 +49,6 @@ import org.apache.http.impl.DefaultHttpServerConnection;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.localserver.ServerTestBase;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
@ -72,27 +69,21 @@ public class TestConnectionAutoRelease extends ServerTestBase {
return new TestSuite(TestConnectionAutoRelease.class);
}
public ThreadSafeClientConnManager createTSCCM(HttpParams params,
SchemeRegistry schreg) {
if (params == null)
params = defaultParams;
public ThreadSafeClientConnManager createTSCCM(SchemeRegistry schreg) {
if (schreg == null)
schreg = supportedSchemes;
return new ThreadSafeClientConnManager(params, schreg);
return new ThreadSafeClientConnManager(schreg);
}
public void testReleaseOnEntityConsumeContent() throws Exception {
HttpParams params = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections
(params, 1);
ConnManagerParams.setMaxConnectionsPerRoute
(params, new ConnPerRouteBean(1));
ThreadSafeClientConnManager mgr = createTSCCM(params, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
mgr.setDefaultMaxPerRoute(1);
mgr.setMaxTotalConnections(1);
// Zero connections in the pool
assertEquals(0, mgr.getConnectionsInPool());
DefaultHttpClient client = new DefaultHttpClient(mgr, params);
DefaultHttpClient client = new DefaultHttpClient(mgr);
// Get some random data
HttpGet httpget = new HttpGet("/random/20000");
@ -124,17 +115,14 @@ public class TestConnectionAutoRelease extends ServerTestBase {
}
public void testReleaseOnEntityWriteTo() throws Exception {
HttpParams params = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections
(params, 1);
ConnManagerParams.setMaxConnectionsPerRoute
(params, new ConnPerRouteBean(1));
ThreadSafeClientConnManager mgr = createTSCCM(params, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
mgr.setDefaultMaxPerRoute(1);
mgr.setMaxTotalConnections(1);
// Zero connections in the pool
assertEquals(0, mgr.getConnectionsInPool());
DefaultHttpClient client = new DefaultHttpClient(mgr, params);
DefaultHttpClient client = new DefaultHttpClient(mgr);
// Get some random data
HttpGet httpget = new HttpGet("/random/20000");
@ -167,17 +155,14 @@ public class TestConnectionAutoRelease extends ServerTestBase {
}
public void testReleaseOnAbort() throws Exception {
HttpParams params = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections
(params, 1);
ConnManagerParams.setMaxConnectionsPerRoute
(params, new ConnPerRouteBean(1));
ThreadSafeClientConnManager mgr = createTSCCM(params, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
mgr.setDefaultMaxPerRoute(1);
mgr.setMaxTotalConnections(1);
// Zero connections in the pool
assertEquals(0, mgr.getConnectionsInPool());
DefaultHttpClient client = new DefaultHttpClient(mgr, params);
DefaultHttpClient client = new DefaultHttpClient(mgr);
// Get some random data
HttpGet httpget = new HttpGet("/random/20000");
@ -242,17 +227,14 @@ public class TestConnectionAutoRelease extends ServerTestBase {
});
HttpParams params = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections
(params, 1);
ConnManagerParams.setMaxConnectionsPerRoute
(params, new ConnPerRouteBean(1));
ThreadSafeClientConnManager mgr = createTSCCM(params, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
mgr.setDefaultMaxPerRoute(1);
mgr.setMaxTotalConnections(1);
// Zero connections in the pool
assertEquals(0, mgr.getConnectionsInPool());
DefaultHttpClient client = new DefaultHttpClient(mgr, params);
DefaultHttpClient client = new DefaultHttpClient(mgr);
// Get some random data
HttpGet httpget = new HttpGet("/dropdead");

View File

@ -44,8 +44,6 @@ import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
@ -110,16 +108,14 @@ public class TestConnectionReuse extends TestCase {
HttpProtocolParams.setUserAgent(params, "TestAgent/1.1");
HttpProtocolParams.setUseExpectContinue(params, false);
HttpConnectionParams.setStaleCheckingEnabled(params, false);
ConnManagerParams.setMaxTotalConnections(params, 5);
ConnManagerParams.setMaxConnectionsPerRoute(params,
new ConnPerRouteBean(5));
SchemeRegistry supportedSchemes = new SchemeRegistry();
SocketFactory sf = PlainSocketFactory.getSocketFactory();
supportedSchemes.register(new Scheme("http", sf, 80));
ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(
params, supportedSchemes);
ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(supportedSchemes);
mgr.setMaxTotalConnections(5);
mgr.setDefaultMaxPerRoute(5);
DefaultHttpClient client = new DefaultHttpClient(mgr, params);
@ -182,16 +178,14 @@ public class TestConnectionReuse extends TestCase {
HttpProtocolParams.setUserAgent(params, "TestAgent/1.1");
HttpProtocolParams.setUseExpectContinue(params, false);
HttpConnectionParams.setStaleCheckingEnabled(params, false);
ConnManagerParams.setMaxTotalConnections(params, 5);
ConnManagerParams.setMaxConnectionsPerRoute(params,
new ConnPerRouteBean(5));
SchemeRegistry supportedSchemes = new SchemeRegistry();
SocketFactory sf = PlainSocketFactory.getSocketFactory();
supportedSchemes.register(new Scheme("http", sf, 80));
ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(
params, supportedSchemes);
ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(supportedSchemes);
mgr.setMaxTotalConnections(5);
mgr.setDefaultMaxPerRoute(5);
DefaultHttpClient client = new DefaultHttpClient(mgr, params);
@ -244,16 +238,14 @@ public class TestConnectionReuse extends TestCase {
HttpProtocolParams.setUserAgent(params, "TestAgent/1.1");
HttpProtocolParams.setUseExpectContinue(params, false);
HttpConnectionParams.setStaleCheckingEnabled(params, false);
ConnManagerParams.setMaxTotalConnections(params, 5);
ConnManagerParams.setMaxConnectionsPerRoute(params,
new ConnPerRouteBean(5));
SchemeRegistry supportedSchemes = new SchemeRegistry();
SocketFactory sf = PlainSocketFactory.getSocketFactory();
supportedSchemes.register(new Scheme("http", sf, 80));
ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(
params, supportedSchemes);
ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(supportedSchemes);
mgr.setMaxTotalConnections(5);
mgr.setDefaultMaxPerRoute(5);
DefaultHttpClient client = new DefaultHttpClient(mgr, params);
@ -307,16 +299,14 @@ public class TestConnectionReuse extends TestCase {
HttpProtocolParams.setUserAgent(params, "TestAgent/1.1");
HttpProtocolParams.setUseExpectContinue(params, false);
HttpConnectionParams.setStaleCheckingEnabled(params, false);
ConnManagerParams.setMaxTotalConnections(params, 1);
ConnManagerParams.setMaxConnectionsPerRoute(params,
new ConnPerRouteBean(1));
SchemeRegistry supportedSchemes = new SchemeRegistry();
SocketFactory sf = PlainSocketFactory.getSocketFactory();
supportedSchemes.register(new Scheme("http", sf, 80));
ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(
params, supportedSchemes);
ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(supportedSchemes);
mgr.setMaxTotalConnections(1);
mgr.setDefaultMaxPerRoute(1);
DefaultHttpClient client = new DefaultHttpClient(mgr, params);
HttpHost target = new HttpHost(saddress.getHostName(), saddress.getPort(), "http");

View File

@ -37,9 +37,6 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.zip.Deflater;
/* Don't use Java 6 functionality, even in tests. */
//import java.util.zip.DeflaterInputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.http.Header;
@ -51,12 +48,10 @@ import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.DeflateDecompressingEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.RequestAcceptEncoding;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
@ -64,9 +59,6 @@ import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.localserver.ServerTestBase;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import org.apache.http.util.EntityUtils;
@ -192,15 +184,13 @@ public class TestContentCodings extends ServerTestBase {
*/
int clients = 100;
HttpParams params = new BasicHttpParams();
ConnManagerParams.setMaxTotalConnections(params, clients);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
final HttpClient httpClient = new DefaultHttpClient(cm, params);
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
cm.setMaxTotalConnections(clients);
final HttpClient httpClient = new DefaultHttpClient(cm);
ExecutorService executor = Executors.newFixedThreadPool(clients);

View File

@ -152,7 +152,7 @@ public class TestDefaultClientRequestDirector extends BasicServerTestBase {
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
SingleClientConnManager conMan = new SingleClientConnManager(new BasicHttpParams(), registry);
SingleClientConnManager conMan = new SingleClientConnManager(registry);
final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>();
final CountDownLatch getLatch = new CountDownLatch(1);
final DefaultHttpClient client = new DefaultHttpClient(conMan, new BasicHttpParams());
@ -192,7 +192,7 @@ public class TestDefaultClientRequestDirector extends BasicServerTestBase {
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
SingleClientConnManager conMan = new SingleClientConnManager(new BasicHttpParams(), registry);
SingleClientConnManager conMan = new SingleClientConnManager(registry);
final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>();
final CountDownLatch getLatch = new CountDownLatch(1);
final CountDownLatch startLatch = new CountDownLatch(1);
@ -240,7 +240,7 @@ public class TestDefaultClientRequestDirector extends BasicServerTestBase {
CountDownLatch connLatch = new CountDownLatch(1);
CountDownLatch awaitLatch = new CountDownLatch(1);
ConnMan4 conMan = new ConnMan4(new BasicHttpParams(), registry, connLatch, awaitLatch);
ConnMan4 conMan = new ConnMan4(registry, connLatch, awaitLatch);
final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>();
final CountDownLatch getLatch = new CountDownLatch(1);
final DefaultHttpClient client = new DefaultHttpClient(conMan, new BasicHttpParams());
@ -297,7 +297,7 @@ public class TestDefaultClientRequestDirector extends BasicServerTestBase {
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ConnMan3 conMan = new ConnMan3(new BasicHttpParams(), registry);
ConnMan3 conMan = new ConnMan3(registry);
DefaultHttpClient client = new DefaultHttpClient(conMan, new BasicHttpParams());
HttpGet httpget = new HttpGet("/a");
@ -351,9 +351,9 @@ public class TestDefaultClientRequestDirector extends BasicServerTestBase {
private final CountDownLatch connLatch;
private final CountDownLatch awaitLatch;
public ConnMan4(HttpParams params, SchemeRegistry schreg,
public ConnMan4(SchemeRegistry schreg,
CountDownLatch connLatch, CountDownLatch awaitLatch) {
super(params, schreg);
super(schreg);
this.connLatch = connLatch;
this.awaitLatch = awaitLatch;
}
@ -398,8 +398,8 @@ public class TestDefaultClientRequestDirector extends BasicServerTestBase {
private ManagedClientConnection allocatedConnection;
private ManagedClientConnection releasedConnection;
public ConnMan3(HttpParams params, SchemeRegistry schreg) {
super(params, schreg);
public ConnMan3(SchemeRegistry schreg) {
super(schreg);
}
@Override

View File

@ -40,11 +40,11 @@ import org.apache.http.client.HttpClient;
import org.apache.http.client.UserTokenHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ManagedClientConnection;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.localserver.ServerTestBase;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
@ -95,14 +95,13 @@ public class TestStatefulConnManagement extends ServerTestBase {
HttpHost target = new HttpHost("localhost", port);
HttpParams params = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections(params, workerCount);
ConnManagerParams.setMaxConnectionsPerRoute(params,
new ConnPerRouteBean(workerCount));
HttpParams params = new BasicHttpParams();
ConnManagerParams.setTimeout(params, 10L);
ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(
params, supportedSchemes);
ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(supportedSchemes);
mgr.setMaxTotalConnections(workerCount);
mgr.setDefaultMaxPerRoute(workerCount);
DefaultHttpClient client = new DefaultHttpClient(mgr, params);

View File

@ -38,12 +38,10 @@ import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.conn.ManagedClientConnection;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.localserver.ServerTestBase;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.util.EntityUtils;
@ -72,14 +70,10 @@ public class TestSCMWithServer extends ServerTestBase {
*
* @return a connection manager to test
*/
public SingleClientConnManager createSCCM(HttpParams params,
SchemeRegistry schreg) {
if (params == null)
params = defaultParams;
public SingleClientConnManager createSCCM(SchemeRegistry schreg) {
if (schreg == null)
schreg = supportedSchemes;
return new SingleClientConnManager(params, schreg);
return new SingleClientConnManager(schreg);
}
/**
@ -87,10 +81,7 @@ public class TestSCMWithServer extends ServerTestBase {
* a connection was aborted.
*/
public void testOpenAfterAbort() throws Exception {
HttpParams mgrpar = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections(mgrpar, 1);
SingleClientConnManager mgr = createSCCM(mgrpar, null);
SingleClientConnManager mgr = createSCCM(null);
final HttpHost target = getServerHttp();
final HttpRoute route = new HttpRoute(target, null, false);
@ -112,10 +103,7 @@ public class TestSCMWithServer extends ServerTestBase {
*/
public void testReleaseConnectionWithTimeLimits() throws Exception {
HttpParams mgrpar = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections(mgrpar, 1);
SingleClientConnManager mgr = createSCCM(mgrpar, null);
SingleClientConnManager mgr = createSCCM(null);
final HttpHost target = getServerHttp();
final HttpRoute route = new HttpRoute(target, null, false);
@ -207,10 +195,7 @@ public class TestSCMWithServer extends ServerTestBase {
public void testCloseExpiredConnections() throws Exception {
HttpParams mgrpar = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections(mgrpar, 1);
SingleClientConnManager mgr = createSCCM(mgrpar, null);
SingleClientConnManager mgr = createSCCM(null);
final HttpHost target = getServerHttp();
final HttpRoute route = new HttpRoute(target, null, false);
@ -235,10 +220,7 @@ public class TestSCMWithServer extends ServerTestBase {
public void testAlreadyLeased() throws Exception {
HttpParams mgrpar = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections(mgrpar, 1);
SingleClientConnManager mgr = createSCCM(mgrpar, null);
SingleClientConnManager mgr = createSCCM(null);
final HttpHost target = getServerHttp();
final HttpRoute route = new HttpRoute(target, null, false);

View File

@ -39,8 +39,6 @@ import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.ClientConnectionRequest;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.conn.ManagedClientConnection;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
@ -51,7 +49,6 @@ import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
/**
* Tests for <code>ThreadSafeClientConnManager</code> that do not require
* a server to communicate with.
@ -98,14 +95,10 @@ public class TestTSCCMNoServer extends TestCase {
*
* @return a connection manager to test
*/
public ThreadSafeClientConnManager createTSCCM(HttpParams params,
SchemeRegistry schreg) {
if (params == null)
params = createDefaultParams();
public ThreadSafeClientConnManager createTSCCM(SchemeRegistry schreg) {
if (schreg == null)
schreg = createSchemeRegistry();
return new ThreadSafeClientConnManager(params, schreg);
return new ThreadSafeClientConnManager(schreg);
}
@ -139,37 +132,28 @@ public class TestTSCCMNoServer extends TestCase {
public void testConstructor() {
HttpParams params = createDefaultParams();
SchemeRegistry schreg = createSchemeRegistry();
ThreadSafeClientConnManager mgr =
new ThreadSafeClientConnManager(params, schreg);
ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(schreg);
assertNotNull(mgr);
mgr.shutdown();
mgr = null;
try {
mgr = new ThreadSafeClientConnManager(null, schreg);
fail("null parameters not detected");
mgr = new ThreadSafeClientConnManager(null);
fail("null parameter not detected");
} catch (IllegalArgumentException iax) {
// expected
} finally {
if (mgr != null)
mgr.shutdown();
}
mgr = null;
mgr = new ThreadSafeClientConnManager(params, schreg);
assertNotNull(mgr);
mgr.shutdown();
mgr = null;
} // testConstructor
public void testGetConnection()
throws InterruptedException, ConnectionPoolTimeoutException {
ThreadSafeClientConnManager mgr = createTSCCM(null, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
HttpHost target = new HttpHost("www.test.invalid", 80, "http");
HttpRoute route = new HttpRoute(target, null, false);
@ -198,11 +182,9 @@ public class TestTSCCMNoServer extends TestCase {
public void testMaxConnTotal()
throws InterruptedException, ConnectionPoolTimeoutException {
HttpParams params = createDefaultParams();
ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(1));
ConnManagerParams.setMaxTotalConnections(params, 2);
ThreadSafeClientConnManager mgr = createTSCCM(params, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
mgr.setMaxTotalConnections(2);
mgr.setDefaultMaxPerRoute(1);
HttpHost target1 = new HttpHost("www.test1.invalid", 80, "http");
HttpRoute route1 = new HttpRoute(target1, null, false);
@ -247,16 +229,11 @@ public class TestTSCCMNoServer extends TestCase {
HttpHost target3 = new HttpHost("www.test3.invalid", 80, "http");
HttpRoute route3 = new HttpRoute(target3, null, false);
HttpParams params = createDefaultParams();
ConnManagerParams.setMaxTotalConnections(params, 100);
ConnPerRouteBean connPerRoute = new ConnPerRouteBean(1);
connPerRoute.setMaxForRoute(route2, 2);
connPerRoute.setMaxForRoute(route3, 3);
ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
ThreadSafeClientConnManager mgr = createTSCCM(params, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
mgr.setMaxTotalConnections(100);
mgr.setDefaultMaxPerRoute(1);
mgr.setMaxForRoute(route2, 2);
mgr.setMaxForRoute(route3, 3);
// route 3, limit 3
ManagedClientConnection conn1 =
@ -318,11 +295,9 @@ public class TestTSCCMNoServer extends TestCase {
public void testReleaseConnection() throws Exception {
HttpParams params = createDefaultParams();
ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(1));
ConnManagerParams.setMaxTotalConnections(params, 3);
ThreadSafeClientConnManager mgr = createTSCCM(params, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
mgr.setMaxTotalConnections(3);
mgr.setDefaultMaxPerRoute(1);
HttpHost target1 = new HttpHost("www.test1.invalid", 80, "http");
HttpRoute route1 = new HttpRoute(target1, null, false);
@ -389,7 +364,7 @@ public class TestTSCCMNoServer extends TestCase {
public void testDeleteClosedConnections()
throws InterruptedException, ConnectionPoolTimeoutException {
ThreadSafeClientConnManager mgr = createTSCCM(null, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
HttpHost target = new HttpHost("www.test.invalid", 80, "http");
HttpRoute route = new HttpRoute(target, null, false);
@ -421,11 +396,9 @@ public class TestTSCCMNoServer extends TestCase {
public void testShutdown() throws Exception {
// 3.x: TestHttpConnectionManager.testShutdown
HttpParams params = createDefaultParams();
ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(1));
ConnManagerParams.setMaxTotalConnections(params, 1);
ThreadSafeClientConnManager mgr = createTSCCM(params, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
mgr.setMaxTotalConnections(1);
mgr.setDefaultMaxPerRoute(1);
HttpHost target = new HttpHost("www.test.invalid", 80, "http");
HttpRoute route = new HttpRoute(target, null, false);
@ -469,10 +442,8 @@ public class TestTSCCMNoServer extends TestCase {
public void testInterruptThread() throws Exception {
// 3.x: TestHttpConnectionManager.testWaitingThreadInterrupted
HttpParams params = createDefaultParams();
ConnManagerParams.setMaxTotalConnections(params, 1);
ThreadSafeClientConnManager mgr = createTSCCM(params, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
mgr.setMaxTotalConnections(1);
HttpHost target = new HttpHost("www.test.invalid", 80, "http");
HttpRoute route = new HttpRoute(target, null, false);
@ -517,10 +488,8 @@ public class TestTSCCMNoServer extends TestCase {
public void testReusePreference() throws Exception {
// 3.x: TestHttpConnectionManager.testHostReusePreference
HttpParams params = createDefaultParams();
ConnManagerParams.setMaxTotalConnections(params, 1);
ThreadSafeClientConnManager mgr = createTSCCM(params, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
mgr.setMaxTotalConnections(1);
HttpHost target1 = new HttpHost("www.test1.invalid", 80, "http");
HttpRoute route1 = new HttpRoute(target1, null, false);
@ -557,10 +526,8 @@ public class TestTSCCMNoServer extends TestCase {
}
public void testAbortAfterRequestStarts() throws Exception {
HttpParams params = createDefaultParams();
ConnManagerParams.setMaxTotalConnections(params, 1);
ThreadSafeClientConnManager mgr = createTSCCM(params, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
mgr.setMaxTotalConnections(1);
HttpHost target = new HttpHost("www.test.invalid", 80, "http");
HttpRoute route = new HttpRoute(target, null, false);
@ -598,15 +565,12 @@ public class TestTSCCMNoServer extends TestCase {
}
public void testAbortBeforeRequestStarts() throws Exception {
HttpParams params = createDefaultParams();
ConnManagerParams.setMaxTotalConnections(params, 1);
ThreadSafeClientConnManager mgr = createTSCCM(params, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
mgr.setMaxTotalConnections(1);
HttpHost target = new HttpHost("www.test.invalid", 80, "http");
HttpRoute route = new HttpRoute(target, null, false);
// get the only connection, then start an extra thread
ManagedClientConnection conn = getConnection(mgr, route, 1L, TimeUnit.MILLISECONDS);
ClientConnectionRequest request = mgr.requestConnection(route, null);

View File

@ -27,7 +27,6 @@
package org.apache.http.impl.conn;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.InetAddress;
@ -53,8 +52,6 @@ import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.conn.ManagedClientConnection;
import org.apache.http.conn.OperatedClientConnection;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
@ -69,7 +66,6 @@ import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
/**
* Tests for <code>ThreadSafeClientConnManager</code> that do require
* a server to communicate with.
@ -114,14 +110,10 @@ public class TestTSCCMWithServer extends ServerTestBase {
*
* @return a connection manager to test
*/
public ThreadSafeClientConnManager createTSCCM(HttpParams params,
SchemeRegistry schreg) {
if (params == null)
params = defaultParams;
public ThreadSafeClientConnManager createTSCCM(SchemeRegistry schreg) {
if (schreg == null)
schreg = supportedSchemes;
return new ThreadSafeClientConnManager(params, schreg);
return new ThreadSafeClientConnManager(schreg);
}
@ -134,12 +126,9 @@ public class TestTSCCMWithServer extends ServerTestBase {
final int COUNT = 8; // adjust to execute more requests
HttpParams mgrpar = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections
(mgrpar, COUNT/2);
ConnManagerParams.setMaxConnectionsPerRoute
(mgrpar, new ConnPerRouteBean(COUNT/2));
ThreadSafeClientConnManager mgr = createTSCCM(mgrpar, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
mgr.setMaxTotalConnections(COUNT/2);
mgr.setDefaultMaxPerRoute(COUNT/2);
final HttpHost target = getServerHttp();
final HttpRoute route = new HttpRoute(target, null, false);
@ -209,10 +198,8 @@ public class TestTSCCMWithServer extends ServerTestBase {
*/
public void testReleaseConnection() throws Exception {
HttpParams mgrpar = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections(mgrpar, 1);
ThreadSafeClientConnManager mgr = createTSCCM(mgrpar, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
mgr.setMaxTotalConnections(1);
final HttpHost target = getServerHttp();
final HttpRoute route = new HttpRoute(target, null, false);
@ -296,10 +283,8 @@ public class TestTSCCMWithServer extends ServerTestBase {
*/
public void testReleaseConnectionWithTimeLimits() throws Exception {
HttpParams mgrpar = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections(mgrpar, 1);
ThreadSafeClientConnManager mgr = createTSCCM(mgrpar, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
mgr.setMaxTotalConnections(1);
final HttpHost target = getServerHttp();
final HttpRoute route = new HttpRoute(target, null, false);
@ -400,10 +385,8 @@ public class TestTSCCMWithServer extends ServerTestBase {
public void testCloseExpiredConnections() throws Exception {
HttpParams mgrpar = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections(mgrpar, 1);
ThreadSafeClientConnManager mgr = createTSCCM(mgrpar, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
mgr.setMaxTotalConnections(1);
final HttpHost target = getServerHttp();
final HttpRoute route = new HttpRoute(target, null, false);
@ -443,10 +426,8 @@ public class TestTSCCMWithServer extends ServerTestBase {
*/
public void testReleaseConnectionOnAbort() throws Exception {
HttpParams mgrpar = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections(mgrpar, 1);
ThreadSafeClientConnManager mgr = createTSCCM(mgrpar, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
mgr.setMaxTotalConnections(1);
final HttpHost target = getServerHttp();
final HttpRoute route = new HttpRoute(target, null, false);
@ -495,7 +476,7 @@ public class TestTSCCMWithServer extends ServerTestBase {
public void testConnectionManagerGC() throws Exception {
// 3.x: TestHttpConnectionManager.testDroppedThread
ThreadSafeClientConnManager mgr = createTSCCM(null, null);
ThreadSafeClientConnManager mgr = createTSCCM(null);
final HttpHost target = getServerHttp();
final HttpRoute route = new HttpRoute(target, null, false);
@ -539,16 +520,14 @@ public class TestTSCCMWithServer extends ServerTestBase {
}
public void testAbortDuringConnecting() throws Exception {
HttpParams mgrpar = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections(mgrpar, 1);
final CountDownLatch connectLatch = new CountDownLatch(1);
final StallingSocketFactory stallingSocketFactory = new StallingSocketFactory(connectLatch, WaitPolicy.BEFORE_CONNECT, PlainSocketFactory.getSocketFactory());
Scheme scheme = new Scheme("http", stallingSocketFactory, 80);
SchemeRegistry registry = new SchemeRegistry();
registry.register(scheme);
ThreadSafeClientConnManager mgr = createTSCCM(mgrpar, registry);
ThreadSafeClientConnManager mgr = createTSCCM(registry);
mgr.setMaxTotalConnections(1);
final HttpHost target = getServerHttp();
final HttpRoute route = new HttpRoute(target, null, false);
@ -603,16 +582,14 @@ public class TestTSCCMWithServer extends ServerTestBase {
}
public void testAbortBeforeSocketCreate() throws Exception {
HttpParams mgrpar = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections(mgrpar, 1);
final CountDownLatch connectLatch = new CountDownLatch(1);
final StallingSocketFactory stallingSocketFactory = new StallingSocketFactory(connectLatch, WaitPolicy.BEFORE_CREATE, PlainSocketFactory.getSocketFactory());
Scheme scheme = new Scheme("http", stallingSocketFactory, 80);
SchemeRegistry registry = new SchemeRegistry();
registry.register(scheme);
ThreadSafeClientConnManager mgr = createTSCCM(mgrpar, registry);
ThreadSafeClientConnManager mgr = createTSCCM(registry);
mgr.setMaxTotalConnections(1);
final HttpHost target = getServerHttp();
final HttpRoute route = new HttpRoute(target, null, false);
@ -669,16 +646,14 @@ public class TestTSCCMWithServer extends ServerTestBase {
}
public void testAbortAfterSocketConnect() throws Exception {
HttpParams mgrpar = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections(mgrpar, 1);
final CountDownLatch connectLatch = new CountDownLatch(1);
final StallingSocketFactory stallingSocketFactory = new StallingSocketFactory(connectLatch, WaitPolicy.AFTER_CONNECT, PlainSocketFactory.getSocketFactory());
Scheme scheme = new Scheme("http", stallingSocketFactory, 80);
SchemeRegistry registry = new SchemeRegistry();
registry.register(scheme);
ThreadSafeClientConnManager mgr = createTSCCM(mgrpar, registry);
ThreadSafeClientConnManager mgr = createTSCCM(registry);
mgr.setMaxTotalConnections(1);
final HttpHost target = getServerHttp();
final HttpRoute route = new HttpRoute(target, null, false);
@ -740,13 +715,10 @@ public class TestTSCCMWithServer extends ServerTestBase {
}
public void testAbortAfterOperatorOpen() throws Exception {
HttpParams mgrpar = defaultParams.copy();
ConnManagerParams.setMaxTotalConnections(mgrpar, 1);
final CountDownLatch connectLatch = new CountDownLatch(1);
final AtomicReference<StallingOperator> operatorRef = new AtomicReference<StallingOperator>();
ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(mgrpar, supportedSchemes) {
ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(supportedSchemes) {
@Override
protected ClientConnectionOperator createConnectionOperator(
SchemeRegistry schreg) {
@ -754,6 +726,7 @@ public class TestTSCCMWithServer extends ServerTestBase {
return operatorRef.get();
}
};
mgr.setMaxTotalConnections(1);
assertNotNull(operatorRef.get());
final HttpHost target = getServerHttp();

View File

@ -36,11 +36,9 @@ import org.apache.http.HttpHost;
import org.apache.http.conn.ClientConnectionOperator;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.conn.DefaultClientConnectionOperator;
import org.apache.http.localserver.ServerTestBase;
import org.apache.http.params.BasicHttpParams;
public class TestConnPoolByRoute extends ServerTestBase {
@ -64,11 +62,8 @@ public class TestConnPoolByRoute extends ServerTestBase {
ClientConnectionOperator operator = new DefaultClientConnectionOperator(
supportedSchemes);
BasicHttpParams params = new BasicHttpParams();
ConnPerRouteBean connPerRoute = new ConnPerRouteBean(3);
ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
ConnPoolByRoute connPool = new ConnPoolByRoute(operator, params);
ConnPoolByRoute connPool = new ConnPoolByRoute(operator, connPerRoute, 20);
try {
// Allocate max possible entries
PoolEntryRequest r1 = connPool.requestPoolEntry(route, null);
@ -111,11 +106,8 @@ public class TestConnPoolByRoute extends ServerTestBase {
ClientConnectionOperator operator = new DefaultClientConnectionOperator(
supportedSchemes);
BasicHttpParams params = new BasicHttpParams();
ConnPerRouteBean connPerRoute = new ConnPerRouteBean(3);
ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
ConnPoolByRoute connPool = new ConnPoolByRoute(operator, params);
ConnPoolByRoute connPool = new ConnPoolByRoute(operator, connPerRoute, 20);
try {
// Allocate max possible entries
PoolEntryRequest r1 = connPool.requestPoolEntry(route, null);

View File

@ -35,7 +35,6 @@ import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.http.HttpHost;
import org.apache.http.HttpVersion;
import org.apache.http.conn.ClientConnectionOperator;
import org.apache.http.conn.ClientConnectionRequest;
import org.apache.http.conn.ManagedClientConnection;
@ -44,17 +43,10 @@ import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SocketFactory;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.conn.params.ConnPerRoute;
// test imports
import org.apache.http.impl.conn.GetConnThread;
/**
* Tests for spurious wakeups in <code>WaitingThread</code>.
* Requires some wrapping code to get at the lock and condition,
@ -93,8 +85,11 @@ public class TestSpuriousWakeup extends TestCase {
protected WaitingThread newestWT;
public XConnPoolByRoute(ClientConnectionOperator operator, HttpParams params) {
super(operator, params);
public XConnPoolByRoute(
final ClientConnectionOperator operator,
final ConnPerRoute connPerRoute,
int maxTotalConnections) {
super(operator, connPerRoute, maxTotalConnections);
}
@Override
@ -118,13 +113,13 @@ public class TestSpuriousWakeup extends TestCase {
protected XConnPoolByRoute extendedCPBR;
public XTSCCM(HttpParams params, SchemeRegistry schreg) {
super(params, schreg);
public XTSCCM(SchemeRegistry schreg) {
super(schreg);
}
@Override
protected AbstractConnPool createConnectionPool(HttpParams params) {
extendedCPBR = new XConnPoolByRoute(connOperator, params);
protected ConnPoolByRoute createConnectionPool() {
extendedCPBR = new XConnPoolByRoute(connOperator, connPerRoute, 20);
// no connection GC required
return extendedCPBR;
}
@ -134,21 +129,15 @@ public class TestSpuriousWakeup extends TestCase {
public void testSpuriousWakeup() throws Exception {
// parameters with connection limit 1
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setUseExpectContinue(params, false);
ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(1));
ConnManagerParams.setMaxTotalConnections(params, 1);
SchemeRegistry schreg = new SchemeRegistry();
SocketFactory sf = PlainSocketFactory.getSocketFactory();
schreg.register(new Scheme("http", sf, 80));
XTSCCM mgr = new XTSCCM(params, schreg);
XTSCCM mgr = new XTSCCM(schreg);
try {
mgr.setMaxTotalConnections(1);
mgr.setDefaultMaxPerRoute(1);
// take out the only connection
ClientConnectionRequest connRequest = mgr.requestConnection(ROUTE, null);
ManagedClientConnection conn = connRequest.getConnection(0, null);

View File

@ -459,12 +459,10 @@ httpclient.setRoutePlanner(new HttpRoutePlanner() {
unintentionally.</para>
<para>This is an example of acquiring a connection from a connection manager:</para>
<programlisting><![CDATA[
HttpParams params = new BasicHttpParams();
Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
SchemeRegistry sr = new SchemeRegistry();
sr.register(http);
ClientConnectionManager connMrg = new SingleClientConnManager(params, sr);
ClientConnectionManager connMrg = new SingleClientConnManager(sr);
// Request new connection. This can be a long process
ClientConnectionRequest connRequest = connMrg.requestConnection(
@ -546,24 +544,22 @@ try {
services. Connection limits, however, can be adjusted using HTTP parameters.</para>
<para>This example shows how the connection pool parameters can be adjusted:</para>
<programlisting><![CDATA[
HttpParams params = new BasicHttpParams();
// Increase max total connection to 200
ConnManagerParams.setMaxTotalConnections(params, 200);
// Increase default max connection per route to 20
ConnPerRouteBean connPerRoute = new ConnPerRouteBean(20);
// Increase max connections for localhost:80 to 50
HttpHost localhost = new HttpHost("locahost", 80);
connPerRoute.setMaxForRoute(new HttpRoute(localhost), 50);
ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(
new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(
new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
HttpClient httpClient = new DefaultHttpClient(cm, params);
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
// Increase max total connection to 200
cm.setMaxTotalConnections(200);
// Increase default max connection per route to 20
cm.setDefaultMaxPerRoute(20);
// Increase max connections for localhost:80 to 50
HttpHost localhost = new HttpHost("locahost", 80);
cm.setMaxForRoute(new HttpRoute(localhost), 50);
HttpClient httpClient = new DefaultHttpClient(cm);
]]></programlisting>
</section>
<section>
@ -601,24 +597,6 @@ httpclient.getConnectionManager().shutdown();
timeout).</para>
</formalpara>
</listitem>
<listitem>
<formalpara>
<title>'http.conn-manager.max-per-route':</title>
<para>defines the maximum number of connections per route. This limit is
interpreted by client connection managers and applies to individual manager
instances. This parameter expects a value of type
<interfacename>ConnPerRoute</interfacename>.</para>
</formalpara>
</listitem>
<listitem>
<formalpara>
<title>'http.conn-manager.max-total':</title>
<para>defines the maximum number of connections in total. This limit is
interpreted by client connection managers and applies to individual manager
instances. This parameter expects a value of type
<classname>java.lang.Integer</classname>.</para>
</formalpara>
</listitem>
</itemizedlist>
</section>
<section>
@ -634,13 +612,12 @@ httpclient.getConnectionManager().shutdown();
If the connection request cannot be serviced within the given time period
<exceptionname>ConnectionPoolTimeoutException</exceptionname> will be thrown.</para>
<programlisting><![CDATA[
HttpParams params = new BasicHttpParams();
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(
new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
HttpClient httpClient = new DefaultHttpClient(cm, params);
ClientConnectionManager cm = new ThreadSafeClientConnManager(schemeRegistry);
HttpClient httpClient = new DefaultHttpClient(cm);
// URIs to perform GETs on
String[] urisToGet = {