Ramesh Papaganti
Ramesh Papaganti

Reputation: 7861

How to close RestHighLevelClient 5.6.X

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?

Upvotes: 0

Views: 1982

Answers (4)

Thierry
Thierry

Reputation: 141

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
} 

Upvotes: 0

chick
chick

Reputation: 1

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

Upvotes: 0

Ramesh Papaganti
Ramesh Papaganti

Reputation: 7861

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

Upvotes: 1

xxxception
xxxception

Reputation: 955

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.

Upvotes: 1

Related Questions