diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java index a2b3f476f6..d555b32beb 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java @@ -734,7 +734,10 @@ public final H2AsyncClientBuilder disableAuthCaching() { * get closed and evicted from the pool. * * @return this instance. + * @deprecated Configure connection keep-alive settings appropriately and use + * {@link #evictExpiredConnections()} instead. */ + @Deprecated public final H2AsyncClientBuilder evictIdleConnections(final TimeValue maxIdleTime) { this.evictIdleConnections = true; this.maxIdleTime = maxIdleTime; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java index d010ac8617..9f68f04cc5 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java @@ -857,7 +857,10 @@ public final HttpAsyncClientBuilder evictExpiredConnections() { * get closed and evicted from the pool. * * @return this instance. + * @deprecated Configure connection keep-alive settings appropriately and use + * {@link #evictExpiredConnections()} instead. */ + @Deprecated public final HttpAsyncClientBuilder evictIdleConnections(final TimeValue maxIdleTime) { this.evictIdleConnections = true; this.maxIdleTime = Args.notNull(maxIdleTime, "Max idle time"); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java index 6639349323..9854d9e77a 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java @@ -765,7 +765,10 @@ public final HttpClientBuilder evictExpiredConnections() { * * @return this instance. * @since 4.4 + * @deprecated Configure connection keep-alive settings appropriately and use + * {@link #evictExpiredConnections()} instead. */ + @Deprecated public final HttpClientBuilder evictIdleConnections(final TimeValue maxIdleTime) { this.evictIdleConnections = true; this.maxIdleTime = Args.notNull(maxIdleTime, "Max idle time"); diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientEvictExpiredConnections.java b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientEvictExpiredConnections.java index 847ceeba47..eb677ee9f7 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientEvictExpiredConnections.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientEvictExpiredConnections.java @@ -27,27 +27,31 @@ package org.apache.hc.client5.http.examples; import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClients; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.hc.core5.http.message.StatusLine; -import org.apache.hc.core5.pool.PoolStats; import org.apache.hc.core5.util.TimeValue; /** - * Example demonstrating how to evict expired and idle connections - * from the connection pool. + * Example demonstrating how to configure connection keep-alive and + * evict expired connections from the connection pool. */ public class ClientEvictExpiredConnections { public static void main(final String[] args) throws Exception { final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(100); + + final RequestConfig requestConfig = RequestConfig.custom() + .setConnectionKeepAlive(TimeValue.ofSeconds(5)) + .build(); try (final CloseableHttpClient httpclient = HttpClients.custom() .setConnectionManager(cm) + .setDefaultRequestConfig(requestConfig) .evictExpiredConnections() - .evictIdleConnections(TimeValue.ofSeconds(5)) .build()) { // create an array of URIs to perform GETs on final String[] urisToGet = { @@ -69,14 +73,6 @@ public static void main(final String[] args) throws Exception { }); } - final PoolStats stats1 = cm.getTotalStats(); - System.out.println("Connections kept alive: " + stats1.getAvailable()); - - // Sleep 10 sec and let the connection evictor do its job - Thread.sleep(10000); - - final PoolStats stats2 = cm.getTotalStats(); - System.out.println("Connections kept alive: " + stats2.getAvailable()); } }