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

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.

@Badger Could you share your thoughts on this.

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