From 52876ef3a4d846c6a2c7456a8f4edd79849cc3a9 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 26 Nov 2023 11:31:42 +0100 Subject: [PATCH] close HTTPClient when running with JDK21 or newer (#70) The Java HTTP client implements AutoCloseable since JDK 21. Closing the client ensures that asynchronous operations and streams are properly terminated. As we support Java 11, we add any old school "finally" wrapper and conditionally close the client when running on a modern platform. --- CHANGELOG.md | 1 + .../jvault/connector/internal/RequestHelper.java | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38a5a0a..e4c5983 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Improvements * Parse timestamps as `ZonedDateTime` instead of `String` representation * Remove redundant `java.base` requirement from _module-info.java_ (#69) +* Close Java HTTP Client when running on Java 21 or later (#70) ### Dependencies * Updated Jackson to 2.16.0 diff --git a/src/main/java/de/stklcode/jvault/connector/internal/RequestHelper.java b/src/main/java/de/stklcode/jvault/connector/internal/RequestHelper.java index ec047dc..1cd4847 100644 --- a/src/main/java/de/stklcode/jvault/connector/internal/RequestHelper.java +++ b/src/main/java/de/stklcode/jvault/connector/internal/RequestHelper.java @@ -363,6 +363,15 @@ public final class RequestHelper implements Serializable { } } catch (CompletionException e) { throw new ConnectionException(Error.CONNECTION, e.getCause()); + } finally { + if (client instanceof AutoCloseable) { + // Close the client, which is supported since JDK21. + try { + ((AutoCloseable) client).close(); + } catch (Exception ignored) { + // Ignore + } + } } }