# Connecting to ElasticSearch via JavaAPI is slow on the first hit

**URL:** <https://discuss.elastic.co/t/connecting-to-elasticsearch-via-javaapi-is-slow-on-the-first-hit/260997>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [January 13, 2021, 12:34pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-via-javaapi-is-slow-on-the-first-hit/260997 "2021-01-13T12:34:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pcvijayvignesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pcvijayvignesh/32/95746_2.png) [@pcvijayvignesh](https://discuss.elastic.co/u/pcvijayvignesh)\
**Post date:** [January 13, 2021, 12:34pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-via-javaapi-is-slow-on-the-first-hit/260997/1 "2021-01-13T12:34:40Z")

</div>

I have a Java server running on one of our on-premise servers and it will connect to Elastic server hosted in AWS.

Whenever a request is made from the Java application it will connect to elastic with the below piece of code. And once querying is complete, the client will be closed.

```auto
HttpHost[] httpValues=null;
if(hostName.contains(",")) {
	String[] hostNames=hostName.split(",");
	httpValues=new HttpHost[hostNames.length];
	for(int i=0;i<hostNames.length;i++) {
		httpValues[i]=new HttpHost(hostNames[i],esPort,method);
	}
}else {
	httpValues=new HttpHost[1];
	httpValues[0]=new HttpHost(hostName,esPort,method);
}
return new RestHighLevelClient(RestClient.builder(httpValues));

```

The issue I face is when the below piece of code gets executes it takes exactly 30 seconds to respond for the first time and the subsequent hits are returning results faster. Any idea what could be the issue

`new RestHighLevelClient(RestClient.builder(httpValues));`

---

<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:** [February 10, 2021, 12:34pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-via-javaapi-is-slow-on-the-first-hit/260997/2 "2021-02-10T12:34:58Z")

</div>

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