Skip to content

Commit 3cf0d69

Browse files
authored
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
1 parent b04c920 commit 3cf0d69

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpJdkSolrClient.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,21 @@ private NamedList<Object> processErrorsAndResponse(
410410

411411
@Override
412412
public void close() throws IOException {
413+
// If used with Java 21+
414+
if (httpClient instanceof AutoCloseable) {
415+
try {
416+
((AutoCloseable) httpClient).close();
417+
} catch (Exception e) {
418+
log.warn("Could not close the http client.", e);
419+
}
420+
}
421+
httpClient = null;
422+
413423
if (shutdownExecutor) {
414424
ExecutorUtil.shutdownAndAwaitTermination(executor);
415425
}
416426
executor = null;
417427

418-
// TODO: Java 21 adds close/autoclosable to HttpClient. We should use it.
419-
httpClient = null;
420-
421428
assert ObjectReleaseTracker.release(this);
422429
}
423430

0 commit comments

Comments
 (0)