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.
This commit is contained in:
Stefan Kalscheuer 2023-11-26 11:31:42 +01:00
parent 62f2249a4d
commit 52876ef3a4
Signed by: stefan
GPG Key ID: 3887EC2A53B55430
2 changed files with 10 additions and 0 deletions

View File

@ -6,6 +6,7 @@
### Improvements ### Improvements
* Parse timestamps as `ZonedDateTime` instead of `String` representation * Parse timestamps as `ZonedDateTime` instead of `String` representation
* Remove redundant `java.base` requirement from _module-info.java_ (#69) * Remove redundant `java.base` requirement from _module-info.java_ (#69)
* Close Java HTTP Client when running on Java 21 or later (#70)
### Dependencies ### Dependencies
* Updated Jackson to 2.16.0 * Updated Jackson to 2.16.0

View File

@ -363,6 +363,15 @@ public final class RequestHelper implements Serializable {
} }
} catch (CompletionException e) { } catch (CompletionException e) {
throw new ConnectionException(Error.CONNECTION, e.getCause()); 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
}
}
} }
} }