Asynchronous Java API question

Hi all,

I'm writing a simple library with Elasticsearch 1.7.5 Java API, and I just run into a problem.

Following is the code I write to invoke the asynchronous index API:

client.prepareIndex(index, type).setSource(source).execute(settings.getIndexResponseListener());

Here settings.getIndexResponseListener() will return an implementation of ActionListener<Response>. As far as I know, the API call will return immediately, and the operations onwards, such as I/O operations against the Elasticsearch cluster, will be performed in the API's own worker threads asynchronously.

My problem is that when the main thread terminates, that is, running through the ending line of the main method, it seems that the Elasticsearch API's own thread pool is closed immediately too. As a result, some documents that haven't yet been indexed by the worker threads will be lost on the Elasticsearch cluster.

So please tell me how to solve this problem. And also I have some other questions.

Will the following method call also interrupt the ongoing and pending operations by API's worker threads?


And also I'm very confused why the executor will be closed as soon as the main thread terminates, since I found this post on StackOverflow, which says, according to my understanding, the runtime will exit only when all user threads (that are not daemon threads) terminate.

Any help, thanks.