# Asynchronous Java API question

**URL:** <https://discuss.elastic.co/t/asynchronous-java-api-question/44902>\
**Category:** Elasticsearch\
**Created:** [March 19, 2016, 3:07pm UTC](https://discuss.elastic.co/t/asynchronous-java-api-question/44902 "2016-03-19T15:07:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bedew](https://avatars.discourse-cdn.com/v4/letter/b/bbe5ce/32.png) [@bedew](https://discuss.elastic.co/u/bedew)\
**Post date:** [March 19, 2016, 3:07pm UTC](https://discuss.elastic.co/t/asynchronous-java-api-question/44902/1 "2016-03-19T15:07:04Z")

</div>

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:

```auto
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?**

```auto
client.close()

```

**And also I'm very confused why the executor will be closed as soon as the main thread terminates** , since I found [this post](http://stackoverflow.com/a/7416103/4062566) 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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:06pm UTC](https://discuss.elastic.co/t/asynchronous-java-api-question/44902/2 "2017-07-05T23:06:52Z")

</div>


