From 4998e3562dab249ca3dc3af853f01f023122290d Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sat, 30 Apr 2011 11:11:51 +0000 Subject: [PATCH] Re-introduced timeout parameter for connection manager operations git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1098098 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/client/params/ClientPNames.java | 11 ++++++++ .../http/client/params/ClientParamBean.java | 7 ++++++ .../http/client/params/HttpClientParams.java | 25 +++++++++++++++++++ .../http/impl/client/DefaultHttpClient.java | 1 + .../impl/client/DefaultRequestDirector.java | 4 +-- 5 files changed, 46 insertions(+), 2 deletions(-) diff --git a/httpclient/src/main/java/org/apache/http/client/params/ClientPNames.java b/httpclient/src/main/java/org/apache/http/client/params/ClientPNames.java index 6a84119e2..67860dd50 100644 --- a/httpclient/src/main/java/org/apache/http/client/params/ClientPNames.java +++ b/httpclient/src/main/java/org/apache/http/client/params/ClientPNames.java @@ -126,5 +126,16 @@ public interface ClientPNames { */ public static final String DEFAULT_HOST = "http.default-host"; + /** + * Defines the timeout in milliseconds used when retrieving an instance of + * {@link org.apache.http.conn.ManagedClientConnection} from the + * {@link org.apache.http.conn.ClientConnectionManager}. + *

+ * This parameter expects a value of type {@link Long}. + *

+ * @since 4.2 + */ + public static final String CONN_MANAGER_TIMEOUT = "http.conn-manager.timeout"; + } diff --git a/httpclient/src/main/java/org/apache/http/client/params/ClientParamBean.java b/httpclient/src/main/java/org/apache/http/client/params/ClientParamBean.java index 794818bc6..e90b73787 100644 --- a/httpclient/src/main/java/org/apache/http/client/params/ClientParamBean.java +++ b/httpclient/src/main/java/org/apache/http/client/params/ClientParamBean.java @@ -96,4 +96,11 @@ public class ClientParamBean extends HttpAbstractParamBean { params.setParameter(ClientPNames.DEFAULT_HOST, host); } + /** + * @since 4.2 + */ + public void setConnectionManagerTimeout(final long timeout) { + params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, timeout); + } + } diff --git a/httpclient/src/main/java/org/apache/http/client/params/HttpClientParams.java b/httpclient/src/main/java/org/apache/http/client/params/HttpClientParams.java index 93fa855d1..9270aa9a0 100644 --- a/httpclient/src/main/java/org/apache/http/client/params/HttpClientParams.java +++ b/httpclient/src/main/java/org/apache/http/client/params/HttpClientParams.java @@ -28,6 +28,7 @@ package org.apache.http.client.params; import org.apache.http.annotation.Immutable; +import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; /** @@ -93,4 +94,28 @@ public class HttpClientParams { params.setParameter(ClientPNames.COOKIE_POLICY, cookiePolicy); } + /** + * @since 4.2 + */ + public static void setConnectionManagerTimeout(final HttpParams params, long timeout) { + if (params == null) { + throw new IllegalArgumentException("HTTP parameters may not be null"); + } + params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, timeout); + } + + /** + * @since 4.2 + */ + public static long getConnectionManagerTimeout(final HttpParams params) { + if (params == null) { + throw new IllegalArgumentException("HTTP parameters may not be null"); + } + Long timeout = (Long) params.getParameter(ClientPNames.CONN_MANAGER_TIMEOUT); + if (timeout != null) { + return timeout.longValue(); + } + return HttpConnectionParams.getConnectionTimeout(params); + } + } diff --git a/httpclient/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java b/httpclient/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java index 032290e38..ebebca766 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java @@ -109,6 +109,7 @@ import org.apache.http.util.VersionInfo; *

  • {@link org.apache.http.client.params.ClientPNames#DEFAULT_HOST}
  • *
  • {@link org.apache.http.client.params.ClientPNames#DEFAULT_HEADERS}
  • *
  • {@link org.apache.http.client.params.ClientPNames#CONNECTION_MANAGER_FACTORY_CLASS_NAME}
  • + *
  • {@link org.apache.http.client.params.ClientPNames#CONN_MANAGER_TIMEOUT}
  • * * * @since 4.0 diff --git a/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java b/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java index d9818f12b..a72dad8f4 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java @@ -126,6 +126,7 @@ import org.apache.http.util.EntityUtils; *
  • {@link org.apache.http.client.params.ClientPNames#VIRTUAL_HOST}
  • *
  • {@link org.apache.http.client.params.ClientPNames#DEFAULT_HOST}
  • *
  • {@link org.apache.http.client.params.ClientPNames#DEFAULT_HEADERS}
  • + *
  • {@link org.apache.http.client.params.ClientPNames#CONN_MANAGER_TIMEOUT}
  • * * * @since 4.0 @@ -360,8 +361,6 @@ public class DefaultRequestDirector implements RequestDirector { RoutedRequest roureq = new RoutedRequest(origWrapper, origRoute); - long timeout = HttpConnectionParams.getConnectionTimeout(params); - boolean reuse = false; boolean done = false; try { @@ -387,6 +386,7 @@ public class DefaultRequestDirector implements RequestDirector { ((AbortableHttpRequest) orig).setConnectionRequest(connRequest); } + long timeout = HttpClientParams.getConnectionManagerTimeout(params); try { managedConn = connRequest.getConnection(timeout, TimeUnit.MILLISECONDS); } catch(InterruptedException interrupted) {