Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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());

}
}
Expand Down