# High thread count with rest client

**URL:** <https://discuss.elastic.co/t/high-thread-count-with-rest-client/335801>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [June 12, 2023, 5:56pm UTC](https://discuss.elastic.co/t/high-thread-count-with-rest-client/335801 "2023-06-12T17:56:15Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tukaram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tukaram/32/118944_2.png) [@Tukaram](https://discuss.elastic.co/u/Tukaram)\
**Post date:** [June 12, 2023, 5:56pm UTC](https://discuss.elastic.co/t/high-thread-count-with-rest-client/335801/1 "2023-06-12T17:56:16Z")

</div>

Hi,  
I am using 7.17.3 stack for elastic.  
Low level Elasticsearch rest client is from 7.15.2  
I see very high waiting thread count when I build the client for each request and do that in try block so it gets auto closed.  
Any Idea why its not getting closed?  
This caused a big blunder.

RestClientBuilder builder = RestClient.builder(  
new HttpHost(config.getHostName(), config.getPort(), "https"));  
Header defaultHeaders =  
new Header{new BasicHeader("Authorization",  
"Bearer " + tokenProvider.getAdfsTokenResponse().getAccessToken())};  
builder.setDefaultHeaders(defaultHeaders);  
return builder.build();

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 12, 2023, 6:13pm UTC](https://discuss.elastic.co/t/high-thread-count-with-rest-client/335801/2 "2023-06-12T18:13:56Z")

</div>

If you are creating a new client each time you want to request an API, please switch to a singleton model.

So you just have one instance when it's running in your app.

Also update the client and the server to the latest version.

Finally, if you are using the high level client, switch to the new client.

---

<div class="post-metadata">

**Author:** ![Tukaram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tukaram/32/118944_2.png) [@Tukaram](https://discuss.elastic.co/u/Tukaram)\
**Post date:** [June 12, 2023, 6:18pm UTC](https://discuss.elastic.co/t/high-thread-count-with-rest-client/335801/3 "2023-06-12T18:18:46Z")

</div>

Thanks for a quick response!  
I have the oauthtoken which I need to guarantee that its latest. Switching to singleton, how I can make sure my ouath token is always latest without building up client again?  
I dont see option to do single put with high level client, is it same as Bulkrequest?

---

<div class="post-metadata">

**Author:** ![Tukaram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tukaram/32/118944_2.png) [@Tukaram](https://discuss.elastic.co/u/Tukaram)\
**Post date:** [June 12, 2023, 6:23pm UTC](https://discuss.elastic.co/t/high-thread-count-with-rest-client/335801/4 "2023-06-12T18:23:53Z")

</div>

Also, why rest client is not getting closed when java is calling close on same.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 12, 2023, 7:16pm UTC](https://discuss.elastic.co/t/high-thread-count-with-rest-client/335801/5 "2023-06-12T19:16:22Z")

</div>

I think there have been an issue in the past with an old version. So you should upgrade anyway.

> how I can make sure my ouath token is always latest without building up client again?

I think you can let it fail and use [Usage | Elasticsearch Java API Client [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_usage.html)

---

<div class="post-metadata">

**Author:** ![Tukaram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tukaram/32/118944_2.png) [@Tukaram](https://discuss.elastic.co/u/Tukaram)\
**Post date:** [June 12, 2023, 9:30pm UTC](https://discuss.elastic.co/t/high-thread-count-with-rest-client/335801/6 "2023-06-12T21:30:33Z")

</div>

Sorry, I didnt get anything from usage.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 12, 2023, 10:13pm UTC](https://discuss.elastic.co/t/high-thread-count-with-rest-client/335801/7 "2023-06-12T22:13:50Z")

</div>

What do you mean? The page is empty and you don't see code examples or you don't understand the page?

---

<div class="post-metadata">

**Author:** ![Tukaram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tukaram/32/118944_2.png) [@Tukaram](https://discuss.elastic.co/u/Tukaram)\
**Post date:** [June 16, 2023, 1:38pm UTC](https://discuss.elastic.co/t/high-thread-count-with-rest-client/335801/8 "2023-06-16T13:38:29Z")

</div>

I am looking for high level client, and as mentioned I dont see any ref to auth bearer token client

---

<div class="post-metadata">

**Author:** ![Tukaram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tukaram/32/118944_2.png) [@Tukaram](https://discuss.elastic.co/u/Tukaram)\
**Post date:** [June 16, 2023, 2:05pm UTC](https://discuss.elastic.co/t/high-thread-count-with-rest-client/335801/9 "2023-06-16T14:05:46Z")

</div>

If I have to use auth token and use singleton rest client, How I can do that without building client again/using singleton as token will expire. any way we can provide token once we built the client?

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [June 16, 2023, 2:50pm UTC](https://discuss.elastic.co/t/high-thread-count-with-rest-client/335801/10 "2023-06-16T14:50:42Z")

</div>

The bearer token can be provided with every request with the `RequestOptions` parameter. This allows to keep one instance of the client object and modify the authentication header whenever needed.

```java
RequestOptions options = RequestOptions.DEFAULT
    .toBuilder()
    .removeHeader("Authentication")
    .addHeader("Authentication", "Bearer " + token)
    .build();

SearchResponse response = client.search(searchReq, options);

```

---

<div class="post-metadata">

**Author:** ![Tukaram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tukaram/32/118944_2.png) [@Tukaram](https://discuss.elastic.co/u/Tukaram)\
**Post date:** [June 16, 2023, 9:07pm UTC](https://discuss.elastic.co/t/high-thread-count-with-rest-client/335801/11 "2023-06-16T21:07:53Z")

</div>

Thanks! can you please tell me option for low level rest client? is this going to help?  
request.addParameter(HttpHeaders.AUTHORIZATION, "Bearer " + getAccessToken());

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [June 17, 2023, 9:06am UTC](https://discuss.elastic.co/t/high-thread-count-with-rest-client/335801/12 "2023-06-17T09:06:13Z")

</div>

Parameters are for query parameters in the request's URL. You can use the same `RequestOption` with the low level rest client:

```java
Request request = new Request("GET", "/");
request.setOptions(options);
Response response = restClient.performRequest(request);

```

---

<div class="post-metadata">

**Author:** ![Tukaram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tukaram/32/118944_2.png) [@Tukaram](https://discuss.elastic.co/u/Tukaram)\
**Post date:** [June 19, 2023, 4:32pm UTC](https://discuss.elastic.co/t/high-thread-count-with-rest-client/335801/13 "2023-06-19T16:32:13Z")

</div>

thanks again!

RestHighLevelClient - I am not able to find this in '  
co.elastic.clients  
elasticsearch-java  
7.17.3  
'  
can you please help?

---

<div class="post-metadata">

**Author:** ![Tukaram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tukaram/32/118944_2.png) [@Tukaram](https://discuss.elastic.co/u/Tukaram)\
**Post date:** [June 22, 2023, 12:52pm UTC](https://discuss.elastic.co/t/high-thread-count-with-rest-client/335801/14 "2023-06-22T12:52:08Z")

</div>

Hi, one more qq. How it will handle reconnections of client if I use singleton rest client. Is it taken care behind the scenes?

---

<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 20, 2023, 12:52pm UTC](https://discuss.elastic.co/t/high-thread-count-with-rest-client/335801/15 "2023-07-20T12:52:12Z")

</div>

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