# Integration of Java API Elasticsearch

**URL:** <https://discuss.elastic.co/t/integration-of-java-api-elasticsearch/81780>\
**Category:** Elasticsearch\
**Created:** [April 10, 2017, 9:04am UTC](https://discuss.elastic.co/t/integration-of-java-api-elasticsearch/81780 "2017-04-10T09:04:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremy\_Cugmas](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@Jeremy\_Cugmas](https://discuss.elastic.co/u/Jeremy_Cugmas)\
**Post date:** [April 10, 2017, 9:04am UTC](https://discuss.elastic.co/t/integration-of-java-api-elasticsearch/81780/1 "2017-04-10T09:04:39Z")

</div>

Hello everyone,

I'm currently integrating the elasticsearch java api in my api-rest project. About it, Im' using the dependency JAX-RS. All is working fine but there is a little problem which bothers me. I will explain it.  
I don't know if I'm doing this in the good way but there is the scenari of a call to my api endpoint using the elasticsarch java api :

- Calling the ressource
- Checking the securityContext (specific to my api rest app)
- Opening the elasticsearch client
- Doing my query by using SearchResponse
- Closing my client
- Return the response

The little issue is located when I'm closing the client, indeed this step takes between 1 and 2 seconds. It's not a big deal but for what I'm using it,e.g datas vizualisation on a front-end dashboard, it's a little bit slow.  
So what I'm aksing to you it's if my scenari is good , if I'm doing right to close the client at this time and if it's normal that it's take a while to close it ?

Thanks in advance and sorry for the english level, forgive the frenchy I am.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 10, 2017, 9:12am UTC](https://discuss.elastic.co/t/integration-of-java-api-elasticsearch/81780/2 "2017-04-10T09:12:14Z")

</div>

Hey,

you should reuse the Elasticsearch client and not create a new one all the time. The client is also thread safe, so you can share it among all your resources.

The reason why closing takes some time is, that it uses a threadpool internally that needs to be shut down (also one of the reasons why you do not want to start a new one all the time).

--Alex

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cugmas](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@Jeremy\_Cugmas](https://discuss.elastic.co/u/Jeremy_Cugmas)\
**Post date:** [April 10, 2017, 9:17am UTC](https://discuss.elastic.co/t/integration-of-java-api-elasticsearch/81780/3 "2017-04-10T09:17:56Z")

</div>

Ok thanks for your quick response but there is a problem for me with your solution :

When do I decide to close the client ? Cause the dashboard I'm designing will be a part of a web interface which will be used by several clients. It won't cause any problems ?

Thanks

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 10, 2017, 9:43am UTC](https://discuss.elastic.co/t/integration-of-java-api-elasticsearch/81780/4 "2017-04-10T09:43:24Z")

</div>

You can close it, when the application closes.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cugmas](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@Jeremy\_Cugmas](https://discuss.elastic.co/u/Jeremy_Cugmas)\
**Post date:** [April 10, 2017, 12:06pm UTC](https://discuss.elastic.co/t/integration-of-java-api-elasticsearch/81780/5 "2017-04-10T12:06:05Z")

</div>

This is a REST API, it's never completely closed cause it has to be always reachable.

---

<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:** [May 8, 2017, 12:20pm UTC](https://discuss.elastic.co/t/integration-of-java-api-elasticsearch/81780/6 "2017-05-08T12:20:45Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
