# ConnectionClosedException: When trying to connect to Elasticsearch using Java High Level Client

**URL:** <https://discuss.elastic.co/t/connectionclosedexception-when-trying-to-connect-to-elasticsearch-using-java-high-level-client/270661>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [April 20, 2021, 7:03am UTC](https://discuss.elastic.co/t/connectionclosedexception-when-trying-to-connect-to-elasticsearch-using-java-high-level-client/270661 "2021-04-20T07:03:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jenisha\_Ramanathan](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jenisha\_Ramanathan](https://discuss.elastic.co/u/Jenisha_Ramanathan)\
**Post date:** [April 20, 2021, 7:03am UTC](https://discuss.elastic.co/t/connectionclosedexception-when-trying-to-connect-to-elasticsearch-using-java-high-level-client/270661/1 "2021-04-20T07:03:26Z")

</div>

Hi Team,

I'm trying to connect Java to https Elasticsearch using Java High Level Client. Getting ConnectionClosedException when trying to check for existing index that are available in elasticsearch.  
And the elasticsearch url is running on port 9202, getting the same error response even with 9200 port.  
The version of both elasticsearch-rest-high-level-client and elasticsearch are same ie ) **7.7.1**

Please find the below **code snippet** ,

public RestHighLevelClient elasticRestClientBuilder(Map\<String, String\> properties) {  
RestHighLevelClient client = null;  
try {  
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");  
if(null != properties.get("keystoreLocation") && null != properties.get("keystorePassword")) {  
TrustManager trustManagers = null;  
trustManagers = getTrustManagers(KeyStore.getDefaultType(),  
new FileInputStream(new File(properties.get("keystoreLocation"))), properties.get("keystorePassword"));  
sslContext.init(null, trustManagers, new java.security.SecureRandom());  
}

```
			CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
			credentialsProvider.setCredentials(AuthScope.ANY,
					new UsernamePasswordCredentials(properties.get(ApplicationConstants.ELASTIC_USERNAME), properties.get(ApplicationConstants.ELASTIC_PASSWORD)));
			RestClientBuilder builder = RestClient
					.builder(new HttpHost(properties.get(ApplicationConstants.ELASTIC_LOAD_BALANCER_URL), Integer.parseInt(properties.get(AttributeConstants.PORT)),
							properties.get(AttributeConstants.HTTP_TYPE)))
					.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
						@Override
						public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
							httpClientBuilder.setSSLContext(sslContext);
							httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
							return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
						}
					});
			client = new RestHighLevelClient(builder);
		} catch (Exception e) {
			logger.debug("Exception in creating RestHighLevelClient for elastic :", e);
		}
		return client;
}

```

Sample part of **elasticsearch.yml configuration file,**

transport.tcp.port: 9300  
http.port: 9200

xpack.security.enabled : true  
xpack.security.transport.ssl.enabled: true  
xpack.security.transport.ssl.verification\_mode: certificate  
xpack.security.transport.ssl.keystore.path: /etc/elasticsearch/elastic-certificates.p12  
xpack.security.transport.ssl.truststore.path: /etc/elasticsearch/elastic-certificates.p12  
xpack.security.http.ssl.enabled: true  
xpack.security.http.ssl.keystore.path: "/etc/elasticsearch/http.p12"

**Error** response when trying to connect to elasticsearch,  
org.apache.http.ConnectionClosedException: Connection closed  
at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:813)  
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:248)  
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:235)  
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1609)  
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1579)  
at org.elasticsearch.client.RestHighLevelClient.exists(RestHighLevelClient.java:853)  
at com.infy.ceh.management.helper.ElasticHelper.isExistingIndex(ElasticHelper.java:278)

Could anyone please guide me to resolve this issue. Thank you in advance.

---

<div class="post-metadata">

**Author:** ![Jenisha\_Ramanathan](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jenisha\_Ramanathan](https://discuss.elastic.co/u/Jenisha_Ramanathan)\
**Post date:** [April 22, 2021, 12:07pm UTC](https://discuss.elastic.co/t/connectionclosedexception-when-trying-to-connect-to-elasticsearch-using-java-high-level-client/270661/2 "2021-04-22T12:07:54Z")

</div>

@Badger Could you share your thoughts on this.

---

<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:** [May 20, 2021, 12:08pm UTC](https://discuss.elastic.co/t/connectionclosedexception-when-trying-to-connect-to-elasticsearch-using-java-high-level-client/270661/3 "2021-05-20T12:08:15Z")

</div>

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