Hello,
I'm unable to close the threadpool that is backing the HighLevelRestClient. Whenever my framework fails to deploy a part of the application or just for a graceful shutdown, I need the threadpool to shutdown to allow the JVM to terminate. This works as expected when I don't use the Elastic client libraries, or only use the synchronous requests.
Here is a reproducer, when invoking syncThenClose from the main method the application will terminate successfully. When invoking asyncThenClose the application won't terminate. 999 is a bogus port, elastic should not be running to generate the connection timeout error.
import org.apache.http.HttpHost;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.client.*;
import org.elasticsearch.client.indices.GetIndexRequest;
import java.io.IOException;
public class ElasticConnectionPoolTest {
public static void main(String[] args) {
// call one or the other and see if the jvm closes or not.
//syncThenClose(); // scenario #1 process exits
asyncThenClose(); // scenario #2 process doesn't exit.
}
private static void syncThenClose() {
var client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 999, "http")));
var indices = client.indices();
try {
indices.exists(new GetIndexRequest("xm"), RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
try {
client.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private static void asyncThenClose() {
var client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 999, "http")));
var indices = client.indices();
indices.existsAsync(new GetIndexRequest("xm"), RequestOptions.DEFAULT, new ActionListener<>() {
@Override
public void onResponse(Boolean aBoolean) {
throw new RuntimeException("use a different port");
}
@Override
public void onFailure(Exception e) {
e.printStackTrace();
try {
client.close();
} catch (Throwable ex) {
ex.printStackTrace();
}
}
});
}
}
Windows 10, Java 11.0.1,
org.Elasticsearch.client:Elasticsearch-rest-high-level-client:7.15.0 (and 7.13.2)
Note: I wasn't able to reproduce this as a junit4 test case due to how the test runner cleans up.