Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing parameters for HTTP client request pool max connections in RestfulClientFactory #288

Merged
merged 1 commit into from Jan 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -47,6 +47,16 @@ public interface IRestfulClientFactory {
*/
public static final int DEFAULT_SOCKET_TIMEOUT = 10000;

/**
* Default value for {@link #getPoolMaxTotal() ()}
*/
public static final int DEFAULT_POOL_MAX = 20;

/**
* Default value for {@link #getPoolMaxPerRoute() }
*/
public static final int DEFAULT_POOL_MAX_PER_ROUTE = DEFAULT_POOL_MAX;

/**
* Gets the connection request timeout, in milliseconds. This is the amount of time that the HTTPClient connection
* pool may wait for an available connection before failing. This setting typically does not need to be adjusted.
Expand Down Expand Up @@ -99,6 +109,22 @@ public interface IRestfulClientFactory {
*/
int getSocketTimeout();

/**
* Gets the maximum number of connections allowed in the pool.
* <p>
* The default value for this setting is defined by {@link #DEFAULT_POOL_MAX}
* </p>
*/
int getPoolMaxTotal();

/**
* Gets the maximum number of connections per route allowed in the pool.
* <p>
* The default value for this setting is defined by {@link #DEFAULT_POOL_MAX_PER_ROUTE}
* </p>
*/
int getPoolMaxPerRoute();

/**
* Instantiates a new client instance
*
Expand Down Expand Up @@ -193,4 +219,19 @@ public interface IRestfulClientFactory {
*/
void setSocketTimeout(int theSocketTimeout);

/**
* Sets the maximum number of connections allowed in the pool.
* <p>
* The default value for this setting is defined by {@link #DEFAULT_POOL_MAX}
* </p>
*/
void setPoolMaxTotal(int thePoolMaxTotal);

/**
* Sets the maximum number of connections per route allowed in the pool.
* <p>
* The default value for this setting is defined by {@link #DEFAULT_POOL_MAX_PER_ROUTE}
* </p>
*/
void setPoolMaxPerRoute(int thePoolMaxPerRoute);
}
Expand Up @@ -68,7 +68,9 @@ public class RestfulClientFactory implements IRestfulClientFactory {
private ServerValidationModeEnum myServerValidationMode = DEFAULT_SERVER_VALIDATION_MODE;
private int mySocketTimeout = DEFAULT_SOCKET_TIMEOUT;
private Set<String> myValidatedServerBaseUrls = Collections.synchronizedSet(new HashSet<String>());

private int myPoolMaxTotal = DEFAULT_POOL_MAX;
private int myPoolMaxPerRoute = DEFAULT_POOL_MAX_PER_ROUTE;

/**
* Constructor
*/
Expand Down Expand Up @@ -100,7 +102,9 @@ public synchronized HttpClient getHttpClient() {
if (myHttpClient == null) {

PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS);

connectionManager.setMaxTotal(myPoolMaxTotal);
connectionManager.setDefaultMaxPerRoute(myPoolMaxPerRoute);

//@formatter:off
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(mySocketTimeout)
Expand Down Expand Up @@ -149,6 +153,16 @@ public int getSocketTimeout() {
return mySocketTimeout;
}

@Override
public int getPoolMaxTotal() {
return myPoolMaxTotal;
}

@Override
public int getPoolMaxPerRoute() {
return myPoolMaxPerRoute;
}

@SuppressWarnings("unchecked")
private <T extends IRestfulClient> T instantiateProxy(Class<T> theClientType, InvocationHandler theInvocationHandler) {
T proxy = (T) Proxy.newProxyInstance(theClientType.getClassLoader(), new Class[] { theClientType }, theInvocationHandler);
Expand Down Expand Up @@ -276,6 +290,18 @@ public synchronized void setSocketTimeout(int theSocketTimeout) {
myHttpClient = null;
}

@Override
public synchronized void setPoolMaxTotal(int thePoolMaxTotal) {
myPoolMaxTotal = thePoolMaxTotal;
myHttpClient = null;
}

@Override
public synchronized void setPoolMaxPerRoute(int thePoolMaxPerRoute) {
myPoolMaxPerRoute = thePoolMaxPerRoute;
myHttpClient = null;
}

@SuppressWarnings("unchecked")
void validateServerBase(String theServerBase, HttpClient theHttpClient, BaseClient theClient) {

Expand Down