High thread count with rest client

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();

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.

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?

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

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

Sorry, I didnt get anything from usage.

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

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

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?

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.

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

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

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

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

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

thanks again!

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

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

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