HTTPCLIENT-1691: HttpClient instance used internally by HC Fluent to take system properties into account by default
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1793761 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Executor.java b/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Executor.java
index 30999bb..c0e92fa 100644
--- a/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Executor.java
+++ b/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Executor.java
@@ -28,10 +28,6 @@
import java.io.IOException;
import java.net.URISyntaxException;
-import java.security.KeyManagementException;
-import java.security.NoSuchAlgorithmException;
-
-import javax.net.ssl.SSLContext;
import org.apache.hc.client5.http.auth.AuthCache;
import org.apache.hc.client5.http.auth.AuthScope;
@@ -42,58 +38,35 @@
import org.apache.hc.client5.http.cookie.CookieStore;
import org.apache.hc.client5.http.impl.auth.BasicAuthCache;
import org.apache.hc.client5.http.impl.auth.BasicScheme;
-import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
+import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.impl.sync.BasicCredentialsProvider;
import org.apache.hc.client5.http.impl.sync.CloseableHttpClient;
import org.apache.hc.client5.http.impl.sync.HttpClientBuilder;
import org.apache.hc.client5.http.protocol.HttpClientContext;
-import org.apache.hc.client5.http.socket.ConnectionSocketFactory;
-import org.apache.hc.client5.http.socket.LayeredConnectionSocketFactory;
-import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
-import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
-import org.apache.hc.client5.http.ssl.SSLInitializationException;
import org.apache.hc.core5.http.HttpHost;
-import org.apache.hc.core5.http.config.Registry;
-import org.apache.hc.core5.http.config.RegistryBuilder;
import org.apache.hc.core5.util.TimeValue;
/**
* An Executor for fluent requests.
* <p>
- * A {@link PoolingHttpClientConnectionManager} with maximum 100 connections per route and
+ * A connection pool with maximum 100 connections per route and
* a total maximum of 200 connections is used internally.
- * </p>
*/
public class Executor {
- final static PoolingHttpClientConnectionManager CONNMGR;
final static CloseableHttpClient CLIENT;
static {
- LayeredConnectionSocketFactory ssl = null;
- try {
- ssl = SSLConnectionSocketFactory.getSystemSocketFactory();
- } catch (final SSLInitializationException ex) {
- final SSLContext sslcontext;
- try {
- sslcontext = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
- sslcontext.init(null, null, null);
- ssl = new SSLConnectionSocketFactory(sslcontext);
- } catch (final SecurityException | NoSuchAlgorithmException | KeyManagementException ignore) {
- }
- }
-
- final Registry<ConnectionSocketFactory> sfr = RegistryBuilder.<ConnectionSocketFactory>create()
- .register("http", PlainConnectionSocketFactory.getSocketFactory())
- .register("https", ssl != null ? ssl : SSLConnectionSocketFactory.getSocketFactory())
- .build();
-
- CONNMGR = new PoolingHttpClientConnectionManager(sfr);
- CONNMGR.setDefaultMaxPerRoute(100);
- CONNMGR.setMaxTotal(200);
- CONNMGR.setValidateAfterInactivity(TimeValue.ofSeconds(1));
CLIENT = HttpClientBuilder.create()
- .setConnectionManager(CONNMGR)
+ .setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create()
+ .useSystemProperties()
+ .setMaxConnPerRoute(100)
+ .setMaxConnTotal(200)
+ .setValidateAfterInactivity(TimeValue.ofSeconds(10))
+ .build())
+ .useSystemProperties()
+ .evictExpiredConnections()
+ .evictIdleConnections(TimeValue.ofMinutes(1))
.build();
}
@@ -270,12 +243,4 @@
return new Response(request.internalExecute(this.httpclient, localContext));
}
- /**
- * Closes all idle persistent connections used by the internal pool.
- * @since 4.4
- */
- public static void closeIdleConnections() {
- CONNMGR.closeIdle(TimeValue.NEG_ONE_MILLISECONDS);
- }
-
}
diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/fluent/TestFluent.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/fluent/TestFluent.java
index 7ea7fd0..a5e6f29 100644
--- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/fluent/TestFluent.java
+++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/fluent/TestFluent.java
@@ -32,7 +32,6 @@
import java.nio.charset.Charset;
import org.apache.hc.client5.http.fluent.Content;
-import org.apache.hc.client5.http.fluent.Executor;
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.client5.http.protocol.ClientProtocolException;
import org.apache.hc.client5.testing.sync.LocalServerTestBase;
@@ -47,7 +46,6 @@
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.protocol.HttpContext;
-import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -93,12 +91,6 @@
});
}
- @After @Override
- public void shutDown() throws Exception {
- Executor.closeIdleConnections();
- super.shutDown();
- }
-
@Test
public void testGetRequest() throws Exception {
final HttpHost target = start();
diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/LocalServerTestBase.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/LocalServerTestBase.java
index aab1627..3f710db 100644
--- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/LocalServerTestBase.java
+++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/LocalServerTestBase.java
@@ -93,7 +93,7 @@
this.httpclient.close();
}
if (this.server != null) {
- this.server.shutdown(ShutdownType.GRACEFUL);
+ this.server.shutdown(ShutdownType.IMMEDIATE);
}
}