Refactored configuration of the thread-safe connection manager and its components. Parameters now apply at the construction time only
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@648877 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
bfbc2e0007
commit
5a08148d25
|
@ -34,13 +34,9 @@ package org.apache.http.conn;
|
|||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.http.params.HttpParams;
|
||||
|
||||
import org.apache.http.conn.routing.HttpRoute;
|
||||
import org.apache.http.conn.scheme.SchemeRegistry;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Management interface for {@link ManagedClientConnection client connections}.
|
||||
*
|
||||
|
@ -57,15 +53,6 @@ import org.apache.http.conn.scheme.SchemeRegistry;
|
|||
*/
|
||||
public interface ClientConnectionManager {
|
||||
|
||||
/**
|
||||
* Obtains the parameters of this manager.
|
||||
*
|
||||
* @return the parameters, never <code>null</code>
|
||||
*/
|
||||
HttpParams getParams()
|
||||
;
|
||||
|
||||
|
||||
/**
|
||||
* Obtains the scheme registry used by this manager.
|
||||
*
|
||||
|
|
|
@ -81,9 +81,6 @@ public class SingleClientConnManager implements ClientConnectionManager {
|
|||
/** The schemes supported by this connection manager. */
|
||||
protected SchemeRegistry schemeRegistry;
|
||||
|
||||
/** The parameters of this connection manager. */
|
||||
protected HttpParams params;
|
||||
|
||||
/** The operator for opening and updating connections. */
|
||||
protected ClientConnectionOperator connOperator;
|
||||
|
||||
|
@ -115,15 +112,10 @@ public class SingleClientConnManager implements ClientConnectionManager {
|
|||
public SingleClientConnManager(HttpParams params,
|
||||
SchemeRegistry schreg) {
|
||||
|
||||
if (params == null) {
|
||||
throw new IllegalArgumentException
|
||||
("Parameters must not be null.");
|
||||
}
|
||||
if (schreg == null) {
|
||||
throw new IllegalArgumentException
|
||||
("Scheme registry must not be null.");
|
||||
}
|
||||
this.params = params;
|
||||
this.schemeRegistry = schreg;
|
||||
this.connOperator = createConnectionOperator(schreg);
|
||||
this.uniquePoolEntry = new PoolEntry(connOperator.createConnection());
|
||||
|
@ -135,11 +127,6 @@ public class SingleClientConnManager implements ClientConnectionManager {
|
|||
} // <constructor>
|
||||
|
||||
|
||||
// non-javadoc, see interface ClientConnectionManager
|
||||
public HttpParams getParams() {
|
||||
return this.params;
|
||||
}
|
||||
|
||||
// non-javadoc, see interface ClientConnectionManager
|
||||
public SchemeRegistry getSchemeRegistry() {
|
||||
return this.schemeRegistry;
|
||||
|
|
|
@ -48,7 +48,6 @@ import org.apache.http.conn.ClientConnectionManager;
|
|||
import org.apache.http.conn.ConnectionPoolTimeoutException;
|
||||
import org.apache.http.conn.OperatedClientConnection;
|
||||
import org.apache.http.conn.routing.HttpRoute;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.impl.conn.IdleConnectionHandler;
|
||||
|
||||
|
||||
|
@ -86,12 +85,6 @@ public abstract class AbstractConnPool implements RefQueueHandler {
|
|||
/** The current total number of connections. */
|
||||
protected int numConnections;
|
||||
|
||||
/** The parameters of this connection pool. */
|
||||
//@@@ allow get/set? synchronized?
|
||||
//@@@ currently needed for connection limits
|
||||
protected HttpParams params;
|
||||
|
||||
|
||||
/**
|
||||
* The connection manager.
|
||||
* This weak reference is used only to detect garbage collection
|
||||
|
@ -141,9 +134,6 @@ public abstract class AbstractConnPool implements RefQueueHandler {
|
|||
* @param mgr the connection manager
|
||||
*/
|
||||
protected AbstractConnPool(ClientConnectionManager mgr) {
|
||||
|
||||
params = mgr.getParams();
|
||||
|
||||
issuedConnections = new HashSet<BasicPoolEntryRef>();
|
||||
idleConnHandler = new IdleConnectionHandler();
|
||||
|
||||
|
|
|
@ -47,6 +47,7 @@ import org.apache.http.conn.ClientConnectionOperator;
|
|||
import org.apache.http.conn.ConnectionPoolTimeoutException;
|
||||
import org.apache.http.conn.params.ConnPerRoute;
|
||||
import org.apache.http.conn.params.HttpConnectionManagerParams;
|
||||
import org.apache.http.params.HttpParams;
|
||||
|
||||
|
||||
|
||||
|
@ -86,19 +87,24 @@ public class ConnPoolByRoute extends AbstractConnPool {
|
|||
*/
|
||||
protected final Map<HttpRoute, RouteSpecificPool> routeToPool;
|
||||
|
||||
|
||||
|
||||
protected final int maxTotalConnections;
|
||||
|
||||
private final ConnPerRoute connPerRoute;
|
||||
|
||||
/**
|
||||
* Creates a new connection pool, managed by route.
|
||||
*
|
||||
* @param mgr the connection manager
|
||||
*/
|
||||
public ConnPoolByRoute(ClientConnectionManager mgr) {
|
||||
public ConnPoolByRoute(final ClientConnectionManager mgr, final HttpParams params) {
|
||||
super(mgr);
|
||||
|
||||
freeConnections = createFreeConnQueue();
|
||||
waitingThreads = createWaitingThreadQueue();
|
||||
routeToPool = createRouteToPoolMap();
|
||||
maxTotalConnections = HttpConnectionManagerParams
|
||||
.getMaxTotalConnections(params);
|
||||
connPerRoute = HttpConnectionManagerParams
|
||||
.getMaxConnectionsPerRoute(params);
|
||||
}
|
||||
|
||||
|
||||
|
@ -142,7 +148,7 @@ public class ConnPoolByRoute extends AbstractConnPool {
|
|||
* @return the new pool
|
||||
*/
|
||||
protected RouteSpecificPool newRouteSpecificPool(HttpRoute route) {
|
||||
return new RouteSpecificPool(route);
|
||||
return new RouteSpecificPool(route, connPerRoute.getMaxForRoute(route));
|
||||
}
|
||||
|
||||
|
||||
|
@ -261,14 +267,6 @@ public class ConnPoolByRoute extends AbstractConnPool {
|
|||
Aborter aborter)
|
||||
throws ConnectionPoolTimeoutException, InterruptedException {
|
||||
|
||||
int maxTotalConnections = HttpConnectionManagerParams
|
||||
.getMaxTotalConnections(this.params);
|
||||
|
||||
ConnPerRoute connPerRoute = HttpConnectionManagerParams
|
||||
.getMaxConnectionsPerRoute(params);
|
||||
|
||||
int maxHostConnections = connPerRoute.getMaxForRoute(route);
|
||||
|
||||
Date deadline = null;
|
||||
if (timeout > 0) {
|
||||
deadline = new Date
|
||||
|
@ -299,12 +297,12 @@ public class ConnPoolByRoute extends AbstractConnPool {
|
|||
if (entry != null) {
|
||||
// we're fine
|
||||
//@@@ yeah this is ugly, but historical... will be revised
|
||||
} else if ((rospl.getEntryCount() < maxHostConnections) &&
|
||||
} else if ((rospl.getEntryCount() < rospl.getMaxEntries()) &&
|
||||
(numConnections < maxTotalConnections)) {
|
||||
|
||||
entry = createEntry(rospl, operator);
|
||||
|
||||
} else if ((rospl.getEntryCount() < maxHostConnections) &&
|
||||
} else if ((rospl.getEntryCount() < rospl.getMaxEntries()) &&
|
||||
(freeConnections.size() > 0)) {
|
||||
|
||||
deleteLeastUsedEntry();
|
||||
|
@ -419,7 +417,7 @@ public class ConnPoolByRoute extends AbstractConnPool {
|
|||
poolLock.lock();
|
||||
try {
|
||||
|
||||
entry = rospl.allocEntry();
|
||||
entry = rospl.allocEntry(state);
|
||||
|
||||
if (entry != null) {
|
||||
if (LOG.isDebugEnabled()) {
|
||||
|
|
|
@ -46,6 +46,9 @@ public class RouteSpecificPool {
|
|||
/** The route this pool is for. */
|
||||
protected final HttpRoute route;
|
||||
|
||||
/** the maximum number of entries allowed for this pool */
|
||||
protected final int maxEntries;
|
||||
|
||||
/**
|
||||
* The list of free entries.
|
||||
* This list is managed LIFO, to increase idle times and
|
||||
|
@ -63,10 +66,12 @@ public class RouteSpecificPool {
|
|||
/**
|
||||
* Creates a new route-specific pool.
|
||||
*
|
||||
* @param r the route for which to pool
|
||||
* @param route the route for which to pool
|
||||
* @param maxEntries the maximum number of entries allowed for this pool
|
||||
*/
|
||||
public RouteSpecificPool(HttpRoute route) {
|
||||
public RouteSpecificPool(HttpRoute route, int maxEntries) {
|
||||
this.route = route;
|
||||
this.maxEntries = maxEntries;
|
||||
this.freeEntries = new LinkedList<BasicPoolEntry>();
|
||||
this.waitingThreads = new LinkedList<WaitingThread>();
|
||||
this.numEntries = 0;
|
||||
|
@ -82,7 +87,17 @@ public class RouteSpecificPool {
|
|||
return route;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Obtains the maximum number of entries allowed for this pool.
|
||||
*
|
||||
* @return the max entry number
|
||||
*/
|
||||
public final int getMaxEntries() {
|
||||
return maxEntries;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Indicates whether this pool is unused.
|
||||
* A pool is unused if there is neither an entry nor a waiting thread.
|
||||
|
@ -113,7 +128,7 @@ public class RouteSpecificPool {
|
|||
*
|
||||
* @return an available pool entry, or <code>null</code> if there is none
|
||||
*/
|
||||
public BasicPoolEntry allocEntry() {
|
||||
public BasicPoolEntry allocEntry(final Object state) {
|
||||
|
||||
BasicPoolEntry entry = null;
|
||||
|
||||
|
|
|
@ -74,9 +74,6 @@ public class ThreadSafeClientConnManager
|
|||
/** The schemes supported by this connection manager. */
|
||||
protected SchemeRegistry schemeRegistry;
|
||||
|
||||
/** The parameters of this connection manager. */
|
||||
protected HttpParams params;
|
||||
|
||||
/** The pool of connections being managed. */
|
||||
protected final AbstractConnPool connectionPool;
|
||||
|
||||
|
@ -96,11 +93,10 @@ public class ThreadSafeClientConnManager
|
|||
SchemeRegistry schreg) {
|
||||
|
||||
if (params == null) {
|
||||
throw new IllegalArgumentException("Parameters must not be null.");
|
||||
throw new IllegalArgumentException("HTTP parameters may not be null");
|
||||
}
|
||||
this.params = params;
|
||||
this.schemeRegistry = schreg;
|
||||
this.connectionPool = createConnectionPool();
|
||||
this.connectionPool = createConnectionPool(params);
|
||||
this.connOperator = createConnectionOperator(schreg);
|
||||
|
||||
} // <constructor>
|
||||
|
@ -111,9 +107,9 @@ public class ThreadSafeClientConnManager
|
|||
*
|
||||
* @return the connection pool to use
|
||||
*/
|
||||
protected AbstractConnPool createConnectionPool() {
|
||||
protected AbstractConnPool createConnectionPool(final HttpParams params) {
|
||||
|
||||
AbstractConnPool acp = new ConnPoolByRoute(this);
|
||||
AbstractConnPool acp = new ConnPoolByRoute(this, params);
|
||||
boolean conngc = true; //@@@ check parameters to decide
|
||||
if (conngc) {
|
||||
acp.enableConnectionGC();
|
||||
|
@ -270,10 +266,5 @@ public class ThreadSafeClientConnManager
|
|||
}
|
||||
|
||||
|
||||
// non-javadoc, see interface ClientConnectionManager
|
||||
public HttpParams getParams() {
|
||||
return this.params;
|
||||
}
|
||||
|
||||
} // class ThreadSafeClientConnManager
|
||||
|
||||
|
|
|
@ -145,15 +145,9 @@ public class TestTSCCMNoServer extends TestCase {
|
|||
HttpParams params = createDefaultParams();
|
||||
SchemeRegistry schreg = createSchemeRegistry();
|
||||
|
||||
final String paramkey = "test.parameter";
|
||||
final String paramval = "Value of the test parameter.";
|
||||
params.setParameter(paramkey, paramval);
|
||||
|
||||
ThreadSafeClientConnManager mgr =
|
||||
new ThreadSafeClientConnManager(params, schreg);
|
||||
assertNotNull(mgr);
|
||||
assertNotNull(mgr.getParams());
|
||||
assertEquals(paramval, mgr.getParams().getParameter(paramkey));
|
||||
mgr.shutdown();
|
||||
mgr = null;
|
||||
|
||||
|
@ -170,8 +164,6 @@ public class TestTSCCMNoServer extends TestCase {
|
|||
|
||||
mgr = new ThreadSafeClientConnManager(params, schreg);
|
||||
assertNotNull(mgr);
|
||||
assertNotNull(mgr.getParams());
|
||||
assertEquals(paramval, mgr.getParams().getParameter(paramkey));
|
||||
mgr.shutdown();
|
||||
mgr = null;
|
||||
|
||||
|
|
|
@ -97,8 +97,8 @@ public class TestSpuriousWakeup extends TestCase {
|
|||
protected WaitingThread newestWT;
|
||||
|
||||
|
||||
public XConnPoolByRoute(ClientConnectionManager mgr) {
|
||||
super(mgr);
|
||||
public XConnPoolByRoute(ClientConnectionManager mgr, HttpParams params) {
|
||||
super(mgr, params);
|
||||
}
|
||||
|
||||
protected synchronized
|
||||
|
@ -125,8 +125,8 @@ public class TestSpuriousWakeup extends TestCase {
|
|||
super(params, schreg);
|
||||
}
|
||||
|
||||
protected AbstractConnPool createConnectionPool() {
|
||||
extendedCPBR = new XConnPoolByRoute(this);
|
||||
protected AbstractConnPool createConnectionPool(HttpParams params) {
|
||||
extendedCPBR = new XConnPoolByRoute(this, params);
|
||||
// no connection GC required
|
||||
return extendedCPBR;
|
||||
}
|
||||
|
|
|
@ -83,7 +83,7 @@ public class TestWaitingThread extends TestCase {
|
|||
assertNull ("thread from nowhere", wt.getThread());
|
||||
|
||||
HttpRoute route = new HttpRoute(TARGET);
|
||||
RouteSpecificPool rospl = new RouteSpecificPool(route);
|
||||
RouteSpecificPool rospl = new RouteSpecificPool(route, 10);
|
||||
wt = new WaitingThread(cnd, rospl);
|
||||
assertEquals("wrong condition", cnd, wt.getCondition());
|
||||
assertEquals("wrong pool", rospl, wt.getPool());
|
||||
|
|
Loading…
Reference in New Issue