From a3989841ae5624a8d989f693fbc30fc26f98e16a Mon Sep 17 00:00:00 2001 From: Igor Balos Date: Tue, 28 Mar 2023 13:01:35 +0200 Subject: [PATCH] http client dependency cleanup and fine tuning connection performance --- CHANGELOG.md | 5 +++++ pom.xml | 4 ++-- .../com/postmarkapp/postmark/client/HttpClient.java | 13 ++++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1c99aa..0bf6d45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 1.10.21 + +* updated default HTTP client connection settings +* adjusted HTTP client library package used + ## 1.10.2 * updated HTTP client data requests issue with unicode characters diff --git a/pom.xml b/pom.xml index bba9570..cbe0a93 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ - 1.10.2 + 1.10.21 UTF-8 1.8 2.9.7 @@ -50,7 +50,7 @@ org.apache.httpcomponents.client5 - httpclient5-fluent + httpclient5 5.2.1 diff --git a/src/main/java/com/postmarkapp/postmark/client/HttpClient.java b/src/main/java/com/postmarkapp/postmark/client/HttpClient.java index e1efa73..5f5bfa4 100644 --- a/src/main/java/com/postmarkapp/postmark/client/HttpClient.java +++ b/src/main/java/com/postmarkapp/postmark/client/HttpClient.java @@ -3,6 +3,7 @@ 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.HttpClientBuilder; +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; import org.apache.hc.core5.http.ClassicHttpRequest; import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.io.entity.EntityUtils; @@ -35,6 +36,8 @@ public enum DEFAULTS { // client configuration options like timeouts, forwarding .. private RequestConfig.Builder clientConfigBuilder; + private final PoolingHttpClientConnectionManager connectionManager; + private boolean secureConnection = true; public HttpClient(Map headers, int connectTimeoutSeconds, int readTimeoutSeconds) { @@ -44,6 +47,10 @@ public HttpClient(Map headers, int connectTimeoutSeconds, int rea .setConnectTimeout(Timeout.ofSeconds(connectTimeoutSeconds)) .setResponseTimeout(Timeout.ofSeconds(readTimeoutSeconds)); + this.connectionManager = new PoolingHttpClientConnectionManager(); + connectionManager.setMaxTotal(100); + connectionManager.setDefaultMaxPerRoute(25); + this.client = buildClient(); } @@ -144,7 +151,11 @@ public CloseableHttpClient getClient() { * @return initialized HTTP client */ private CloseableHttpClient buildClient() { - return HttpClientBuilder.create().setDefaultRequestConfig(clientConfigBuilder.build()).build(); + return HttpClientBuilder + .create() + .setDefaultRequestConfig(clientConfigBuilder.build()) + .setConnectionManager(connectionManager) + .build(); } /**