diff --git a/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java b/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java index 8c480ae21..71602f1ab 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java @@ -34,6 +34,7 @@ import java.util.Collection; import java.util.Collections; import java.util.LinkedList; import java.util.List; +import java.util.concurrent.TimeUnit; import java.util.Map; import javax.net.ssl.SSLContext; @@ -201,6 +202,9 @@ public class HttpClientBuilder { private int maxConnTotal = 0; private int maxConnPerRoute = 0; + private long connTimeToLive = -1; + private TimeUnit connTimeToLiveTimeUnit = TimeUnit.MILLISECONDS; + private List closeables; static final String DEFAULT_USER_AGENT; @@ -309,6 +313,20 @@ public class HttpClientBuilder { return this; } + /** + * Sets maximum time to live for persistent connections + *

+ * Please note this value can be overridden by the {@link #setConnectionManager( + * org.apache.http.conn.HttpClientConnectionManager)} method. + * + * @since 4.4 + */ + public final HttpClientBuilder setConnectionTimeToLive(final long connTimeToLive, final TimeUnit connTimeToLiveTimeUnit) { + this.connTimeToLive = connTimeToLive; + this.connTimeToLiveTimeUnit = connTimeToLiveTimeUnit; + return this; + } + /** * Assigns {@link HttpClientConnectionManager} instance. */ @@ -743,7 +761,12 @@ public class HttpClientBuilder { RegistryBuilder.create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", sslSocketFactoryCopy) - .build()); + .build(), + null, + null, + null, + connTimeToLive, + connTimeToLiveTimeUnit != null ? connTimeToLiveTimeUnit : TimeUnit.MILLISECONDS); if (defaultSocketConfig != null) { poolingmgr.setDefaultSocketConfig(defaultSocketConfig); }