# High Level Rest Client Java initialisation slowing down the application

**URL:** <https://discuss.elastic.co/t/high-level-rest-client-java-initialisation-slowing-down-the-application/326044>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [February 21, 2023, 9:42am UTC](https://discuss.elastic.co/t/high-level-rest-client-java-initialisation-slowing-down-the-application/326044 "2023-02-21T09:42:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anis.tajouri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anis.tajouri/32/116195_2.png) [@anis.tajouri](https://discuss.elastic.co/u/anis.tajouri)\
**Post date:** [February 21, 2023, 9:42am UTC](https://discuss.elastic.co/t/high-level-rest-client-java-initialisation-slowing-down-the-application/326044/1 "2023-02-21T09:42:37Z")

</div>

Using following java code initialising the High Level Rest Client in a web service, without performing any elasticsearch request, I have additional 250ms when calling the web service. commenting this code, I have less avarage 250ms in the timing of request to the web service.  
Do you know any reason about this performance ?  
Do you recommand to use Elasticsearch Java API Client 8.x for better performance ?  
using native HttpsURLConnection give better on whole performance in application but not elasticsearch requests, do you recommand it ?

```auto
            KeyStore clientStore = KeyStore.getInstance("PKCS12");
            clientStore.load(new FileInputStream(config.searchKeyStoreFileName), config.searchKeyStorePassword.toCharArray());

            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(clientStore, config.searchKeyStorePassword.toCharArray());

            KeyStore trustStore = KeyStore.getInstance("PKCS12");
            trustStore.load(new FileInputStream(config.searchTrustStoreFileName), config.searchTrustStorePassword.toCharArray());

            TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            tmf.init(trustStore);

            sslContext = SSLContext.getInstance("TLSv1.2");
            sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            //credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(config.searchUser, config.searchPassword));
            credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(config.searchUser, config.searchPassword));
            RestClientBuilder builder = RestClient.builder(searchServers.get(0), searchServers.get(1));

            builder.setHttpClientConfigCallback(clientBuilder -> {
                clientBuilder.setSSLContext(sslContext);
                clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                return clientBuilder;
            });

            builder.setRequestConfigCallback(requestConfigBuilder -> {
                requestConfigBuilder.setSocketTimeout(1000);
                return requestConfigBuilder;
            });

            restClient = builder.build();

```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [February 21, 2023, 10:07am UTC](https://discuss.elastic.co/t/high-level-rest-client-java-initialisation-slowing-down-the-application/326044/2 "2023-02-21T10:07:24Z")

</div>

Generally clients are initialised at application startup and then reused throughout the life of the application, allowing long running connections to be used. Setting up connections should therefore not affect request latency.

---

<div class="post-metadata">

**Author:** ![anis.tajouri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anis.tajouri/32/116195_2.png) [@anis.tajouri](https://discuss.elastic.co/u/anis.tajouri)\
**Post date:** [February 21, 2023, 10:34am UTC](https://discuss.elastic.co/t/high-level-rest-client-java-initialisation-slowing-down-the-application/326044/3 "2023-02-21T10:34:45Z")

</div>

Thanks ! I missed something in the code and it's fixed.

---

<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:** [March 21, 2023, 10:35am UTC](https://discuss.elastic.co/t/high-level-rest-client-java-initialisation-slowing-down-the-application/326044/4 "2023-03-21T10:35:29Z")

</div>

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