You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by jd...@apache.org on 2024/03/21 13:52:47 UTC

(solr) branch main updated: HttpJdkSolrClient: Close the underlying http client, if it is supported (java 21+) (#2362)

This is an automated email from the ASF dual-hosted git repository.

jdyer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git


The following commit(s) were added to refs/heads/main by this push:
     new 3cf0d6908f3 HttpJdkSolrClient: Close the underlying http client, if it is supported (java 21+) (#2362)
3cf0d6908f3 is described below

commit 3cf0d6908f3a44402baba24292c0f2e239f7d494
Author: James Dyer <jd...@apache.org>
AuthorDate: Thu Mar 21 08:52:41 2024 -0500

    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
---
 .../apache/solr/client/solrj/impl/HttpJdkSolrClient.java    | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

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 63d1d3ca207..35e56ae2a55 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 @@ public class HttpJdkSolrClient extends HttpSolrClientBase {
 
   @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);
   }