HttpJdkSolrClient: Close the underlying http client, if it is supported (java 21+) (#2362)
* Close the underlying http client, if it is supported (java 21+)
* tidy
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpJdkSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpJdkSolrClient.java
index 63d1d3c..35e56ae 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpJdkSolrClient.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpJdkSolrClient.java
@@ -410,14 +410,21 @@
@Override
public void close() throws IOException {
+ // If used with Java 21+
+ if (httpClient instanceof AutoCloseable) {
+ try {
+ ((AutoCloseable) httpClient).close();
+ } catch (Exception e) {
+ log.warn("Could not close the http client.", e);
+ }
+ }
+ httpClient = null;
+
if (shutdownExecutor) {
ExecutorUtil.shutdownAndAwaitTermination(executor);
}
executor = null;
- // TODO: Java 21 adds close/autoclosable to HttpClient. We should use it.
- httpClient = null;
-
assert ObjectReleaseTracker.release(this);
}