From 35e3db695d4134ad6cd30ac10d9183c3b0337dcb Mon Sep 17 00:00:00 2001 From: Igor Balos Date: Mon, 18 Dec 2023 14:24:30 +0100 Subject: [PATCH 1/8] simplify connection manager and update client version --- pom.xml | 2 +- .../postmarkapp/postmark/client/HttpClient.java | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index 6dab4ac..3615a60 100644 --- a/pom.xml +++ b/pom.xml @@ -51,7 +51,7 @@ org.apache.httpcomponents.client5 httpclient5 - 5.2.1 + 5.3 diff --git a/src/main/java/com/postmarkapp/postmark/client/HttpClient.java b/src/main/java/com/postmarkapp/postmark/client/HttpClient.java index 5f5bfa4..1995d6d 100644 --- a/src/main/java/com/postmarkapp/postmark/client/HttpClient.java +++ b/src/main/java/com/postmarkapp/postmark/client/HttpClient.java @@ -2,8 +2,9 @@ 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.client5.http.impl.classic.HttpClients; +import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager; +import org.apache.hc.client5.http.io.HttpClientConnectionManager; import org.apache.hc.core5.http.ClassicHttpRequest; import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.io.entity.EntityUtils; @@ -36,7 +37,7 @@ public enum DEFAULTS { // client configuration options like timeouts, forwarding .. private RequestConfig.Builder clientConfigBuilder; - private final PoolingHttpClientConnectionManager connectionManager; + private final HttpClientConnectionManager connectionManager; private boolean secureConnection = true; @@ -47,9 +48,7 @@ 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.connectionManager = new BasicHttpClientConnectionManager(); this.client = buildClient(); } @@ -151,8 +150,8 @@ public CloseableHttpClient getClient() { * @return initialized HTTP client */ private CloseableHttpClient buildClient() { - return HttpClientBuilder - .create() + return HttpClients + .custom() .setDefaultRequestConfig(clientConfigBuilder.build()) .setConnectionManager(connectionManager) .build(); From 5247369422511c60f6090fb26d5c4661812e126f Mon Sep 17 00:00:00 2001 From: Igor Balos Date: Mon, 18 Dec 2023 14:46:27 +0100 Subject: [PATCH 2/8] use the http client defaults --- .../java/com/postmarkapp/postmark/client/HttpClient.java | 5 ----- src/test/java/integration/MessageStreamsTest.java | 6 +++--- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/postmarkapp/postmark/client/HttpClient.java b/src/main/java/com/postmarkapp/postmark/client/HttpClient.java index 1995d6d..8b498a3 100644 --- a/src/main/java/com/postmarkapp/postmark/client/HttpClient.java +++ b/src/main/java/com/postmarkapp/postmark/client/HttpClient.java @@ -37,8 +37,6 @@ public enum DEFAULTS { // client configuration options like timeouts, forwarding .. private RequestConfig.Builder clientConfigBuilder; - private final HttpClientConnectionManager connectionManager; - private boolean secureConnection = true; public HttpClient(Map headers, int connectTimeoutSeconds, int readTimeoutSeconds) { @@ -48,8 +46,6 @@ public HttpClient(Map headers, int connectTimeoutSeconds, int rea .setConnectTimeout(Timeout.ofSeconds(connectTimeoutSeconds)) .setResponseTimeout(Timeout.ofSeconds(readTimeoutSeconds)); - this.connectionManager = new BasicHttpClientConnectionManager(); - this.client = buildClient(); } @@ -153,7 +149,6 @@ private CloseableHttpClient buildClient() { return HttpClients .custom() .setDefaultRequestConfig(clientConfigBuilder.build()) - .setConnectionManager(connectionManager) .build(); } diff --git a/src/test/java/integration/MessageStreamsTest.java b/src/test/java/integration/MessageStreamsTest.java index 1883ebe..456c07f 100644 --- a/src/test/java/integration/MessageStreamsTest.java +++ b/src/test/java/integration/MessageStreamsTest.java @@ -63,16 +63,16 @@ void edit() throws PostmarkException, IOException { @Test void archive() throws PostmarkException, IOException { - String streamId = "bulk-test-stream-1201"; + String streamId = "test-bulk-test-stream-1201"; MessageStreams messages = client.getMessageStreams(Parameters.init().build("messageStreamType", "all") .build("includeArchivedStreams", "true")); MessageStream foundStream = findMessageStream(streamId); if (foundStream == null) { - client.createMessageStream(new MessageStream(streamId, streamId,"Broadcasts")); + MessageStream messageStream = new MessageStream(streamId, streamId,"Broadcasts"); + client.createMessageStream(messageStream); } - else if (foundStream.getArchivedAt() != null) { client.unarchiveMessageStream(streamId); } From d0da383ea2cca945bd88393df45864e10951a6e3 Mon Sep 17 00:00:00 2001 From: Igor Balos Date: Mon, 18 Dec 2023 16:15:41 +0100 Subject: [PATCH 3/8] revert some of the changes --- .../postmarkapp/postmark/client/HttpClient.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/postmarkapp/postmark/client/HttpClient.java b/src/main/java/com/postmarkapp/postmark/client/HttpClient.java index 8b498a3..fd1bc01 100644 --- a/src/main/java/com/postmarkapp/postmark/client/HttpClient.java +++ b/src/main/java/com/postmarkapp/postmark/client/HttpClient.java @@ -2,9 +2,9 @@ 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.classic.HttpClients; -import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager; -import org.apache.hc.client5.http.io.HttpClientConnectionManager; +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; @@ -14,6 +14,8 @@ import java.io.IOException; import java.util.Map; + + /** * Base HTTP client class solely responsible for making * client requests and returning simple HTTP response. @@ -37,6 +39,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) { @@ -46,6 +50,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(); } @@ -146,9 +154,10 @@ public CloseableHttpClient getClient() { * @return initialized HTTP client */ private CloseableHttpClient buildClient() { - return HttpClients - .custom() + return HttpClientBuilder + .create() .setDefaultRequestConfig(clientConfigBuilder.build()) + .setConnectionManager(connectionManager) .build(); } From 4d76cf74449c44b37d393563fa29ced067aecfd7 Mon Sep 17 00:00:00 2001 From: Igor Balos Date: Mon, 18 Dec 2023 16:24:52 +0100 Subject: [PATCH 4/8] remove obsolete test --- src/test/java/integration/MessageTest.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/test/java/integration/MessageTest.java b/src/test/java/integration/MessageTest.java index 8a00a4f..b523854 100644 --- a/src/test/java/integration/MessageTest.java +++ b/src/test/java/integration/MessageTest.java @@ -31,14 +31,6 @@ void send() throws PostmarkException, IOException { assertEquals(response.getMessageId().length(),36); } - @Test - void invalidMessagetoSend() throws PostmarkException, IOException { - Message message = new Message("from@example.com", null, "Hello from Postmark!", "Hello body"); - - Throwable exception = assertThrows(InvalidMessageException.class, () -> client.deliverMessage(message)); - assertEquals("Zero recipients specified", exception.getMessage()); - } - @Test void invalidApiToken() throws PostmarkException, IOException { ApiClient client = Postmark.getApiClient("1991892", true); From 36441842ff5586c2be8ea7051d9805dc96e11f2c Mon Sep 17 00:00:00 2001 From: Igor Balos Date: Mon, 18 Dec 2023 17:46:55 +0100 Subject: [PATCH 5/8] release http entity resorces once we get back the response and handle it --- .../com/postmarkapp/postmark/client/HttpClient.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/postmarkapp/postmark/client/HttpClient.java b/src/main/java/com/postmarkapp/postmark/client/HttpClient.java index fd1bc01..fd4a54f 100644 --- a/src/main/java/com/postmarkapp/postmark/client/HttpClient.java +++ b/src/main/java/com/postmarkapp/postmark/client/HttpClient.java @@ -3,10 +3,10 @@ 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.classic.HttpClients; 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.HttpEntity; import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.hc.core5.http.io.support.ClassicRequestBuilder; import org.apache.hc.core5.util.Timeout; @@ -92,7 +92,14 @@ public ClientResponse execute(REQUEST_TYPES requestType, String url, String data return client.execute( request, - response -> new ClientResponse(response.getCode(), EntityUtils.toString(response.getEntity()))); + response -> { + final HttpEntity entity = response.getEntity(); + int code = response.getCode(); + String body = EntityUtils.toString(response.getEntity()); + EntityUtils.consume(entity); + + return new ClientResponse(code, body); + }); } /** From ea0233c159c5c1c1c1437b436c53610f56ce9b36 Mon Sep 17 00:00:00 2001 From: Igor Balos Date: Tue, 19 Dec 2023 11:10:20 +0100 Subject: [PATCH 6/8] use the pooling defaults --- .../java/com/postmarkapp/postmark/client/HttpClient.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/java/com/postmarkapp/postmark/client/HttpClient.java b/src/main/java/com/postmarkapp/postmark/client/HttpClient.java index fd4a54f..d83e522 100644 --- a/src/main/java/com/postmarkapp/postmark/client/HttpClient.java +++ b/src/main/java/com/postmarkapp/postmark/client/HttpClient.java @@ -39,8 +39,6 @@ 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) { @@ -50,10 +48,6 @@ 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(); } @@ -164,7 +158,7 @@ private CloseableHttpClient buildClient() { return HttpClientBuilder .create() .setDefaultRequestConfig(clientConfigBuilder.build()) - .setConnectionManager(connectionManager) + .setConnectionManager(new PoolingHttpClientConnectionManager()) .build(); } From ae63f8cebeb0e980bf12834ee6c448f57c3ad176 Mon Sep 17 00:00:00 2001 From: Igor Balos Date: Tue, 19 Dec 2023 11:52:13 +0100 Subject: [PATCH 7/8] updated config, simplified defaults for http client --- .../java/com/postmarkapp/postmark/client/HttpClient.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/postmarkapp/postmark/client/HttpClient.java b/src/main/java/com/postmarkapp/postmark/client/HttpClient.java index d83e522..22815b0 100644 --- a/src/main/java/com/postmarkapp/postmark/client/HttpClient.java +++ b/src/main/java/com/postmarkapp/postmark/client/HttpClient.java @@ -3,7 +3,6 @@ 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.HttpEntity; @@ -14,8 +13,6 @@ import java.io.IOException; import java.util.Map; - - /** * Base HTTP client class solely responsible for making * client requests and returning simple HTTP response. @@ -46,6 +43,8 @@ public HttpClient(Map headers, int connectTimeoutSeconds, int rea this.clientConfigBuilder = RequestConfig .custom() .setConnectTimeout(Timeout.ofSeconds(connectTimeoutSeconds)) + .setConnectionRequestTimeout(Timeout.ofSeconds(connectTimeoutSeconds)) + .setConnectionKeepAlive(Timeout.ofSeconds(connectTimeoutSeconds)) .setResponseTimeout(Timeout.ofSeconds(readTimeoutSeconds)); this.client = buildClient(); @@ -158,7 +157,6 @@ private CloseableHttpClient buildClient() { return HttpClientBuilder .create() .setDefaultRequestConfig(clientConfigBuilder.build()) - .setConnectionManager(new PoolingHttpClientConnectionManager()) .build(); } From 9b4cd11d157b6c3d1dfcf2de5d2ebc5037d16c35 Mon Sep 17 00:00:00 2001 From: Igor Balos Date: Tue, 19 Dec 2023 11:56:10 +0100 Subject: [PATCH 8/8] update version --- CHANGELOG.md | 4 ++++ pom.xml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d2dece..a1aec88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.11.1 + +* small updated to http client connection settings + ## 1.11.0 * Add data removal endpoints diff --git a/pom.xml b/pom.xml index 3615a60..ae3505c 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ - 1.11.0 + 1.11.1 UTF-8 1.8 2.9.7