How to close RestHighLevelClient 5.6.X

1.8k Views Asked by At

Below are the maven version we are using

 <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>5.6.13</version>
  </dependency>


    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>5.6.13</version>
    </dependency>



 private RestHighLevelClient buildRestClient(ElasticRequestVO elasticRequestVO) {
    String elasticHost =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticHost()) ? elasticRequestVO.getElasticHost()
                    : elasticSearchHost;
    int elasticPort =
            (elasticRequestVO.getElasticPort() != 0) ? elasticRequestVO.getElasticPort() : elasticSearchPort;
    String elasticUser =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticUser()) ? elasticRequestVO.getElasticUser()
                    : elasticSearchUser;
    String elasticPassword =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticPassword()) ? elasticRequestVO.getElasticPassword()
                    : elasticSearchPassword;
    HttpHost host = new HttpHost(elasticHost, elasticPort);
    RestClientBuilder restClientBuilder = RestClient.builder(host);

    Optional<String> encodedAuth = getAuthenticationHeader(elasticUser, elasticPassword);

    if (encodedAuth.isPresent()) {
        Header[] requestHeaders =
                new Header[] {new BasicHeader(org.apache.http.HttpHeaders.AUTHORIZATION, encodedAuth.get())};
        restClientBuilder.setDefaultHeaders(requestHeaders);
    }
    return new RestHighLevelClient(restClientBuilder.build());
}

How to close RestHighLevelClient?

4

There are 4 best solutions below

0
Ramesh Papaganti On BEST ANSWER

Insted of returning RestHighLevelClient, retun RestClient from buildRestClient()

Here if code sample

private RestClient buildRestClient(ElasticRequestVO elasticRequestVO) {
    String elasticHost =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticHost()) ? elasticRequestVO.getElasticHost()
                    : elasticSearchHost;
    int elasticPort =
            (elasticRequestVO.getElasticPort() != 0) ? elasticRequestVO.getElasticPort() : elasticSearchPort;
    String elasticUser =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticUser()) ? elasticRequestVO.getElasticUser()
                    : elasticSearchUser;
    String elasticPassword =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticPassword()) ? elasticRequestVO.getElasticPassword()
                    : elasticSearchPassword;
    HttpHost host = new HttpHost(elasticHost, elasticPort);
    RestClientBuilder restClientBuilder = RestClient.builder(host);

    Optional<String> encodedAuth = getAuthenticationHeader(elasticUser, elasticPassword);

    if (encodedAuth.isPresent()) {
        Header[] requestHeaders =
                new Header[] {new BasicHeader(org.apache.http.HttpHeaders.AUTHORIZATION, encodedAuth.get())};
        restClientBuilder.setDefaultHeaders(requestHeaders);
    }
    return restClientBuilder.build();
}
1
xxxception On

If you will see how closing is implemented in one of the latest versions, e.g. 6.2.4 you will found that the RestHighLevelClient just releases the RestClient.
So for closing I can offer you just invoke the restHighLevelClient.getLowLevelClient() and close the rest client lowLevelRestClient.close().

P.S. If just look at the source code would notice that this is just a wrapper over the rest client.

0
chick On

The version of my RestHighLevelClient is 5.6.3,and I can't find method like restHighLevelClient.getLowLevelClient() or restHighLevelClient.close(),so I use reflection to get the restClient to close

Field restClientField = RestHighLevelClient.class.getDeclaredField("client");
restClientField.setAccessible(true);
RestClient restclient = (RestClient)restClientField.get(restHighLevelClient);
restClient.close();
0
Thierry On

I have the same problem. To close the connexion you must change your method :

private RestClient buildRestClient(ElasticRequestVO elasticRequestVO) {
    String elasticHost =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticHost()) ? elasticRequestVO.getElasticHost()
                    : elasticSearchHost;
    int elasticPort =
            (elasticRequestVO.getElasticPort() != 0) ? elasticRequestVO.getElasticPort() : elasticSearchPort;
    String elasticUser =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticUser()) ? elasticRequestVO.getElasticUser()
                    : elasticSearchUser;
    String elasticPassword =
            StringUtils.isNotEmpty(elasticRequestVO.getElasticPassword()) ? elasticRequestVO.getElasticPassword()
                    : elasticSearchPassword;
    HttpHost host = new HttpHost(elasticHost, elasticPort);
    RestClientBuilder restClientBuilder = RestClient.builder(host);

    Optional<String> encodedAuth = getAuthenticationHeader(elasticUser, elasticPassword);

    if (encodedAuth.isPresent()) {
        Header[] requestHeaders =
                new Header[] {new BasicHeader(org.apache.http.HttpHeaders.AUTHORIZATION, encodedAuth.get())};
        restClientBuilder.setDefaultHeaders(requestHeaders);
    }
    return restClientBuilder.build();
}

Then, when you call your method you can work with

final RestClient restClient=buildRestClient(...); 

and do

new RestHighLevelClient(restClient) 

when needed. At the end, you close.

Exemple

try (final RestClient restClient=buildRestClient(...)) {
final RestHighLevelClient restHLClient = new RestHighLevelClient(restClient);
// what you want
}