org.apache.http.ConnectionClosedException: Connection is closed

org.apache.http.ConnectionClosedException: Connection is closed
at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:920)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:300)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:288)
at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:154)
at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1887)
at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1904)getting this error in Java spring boot occasionally for elasticsearch-java:8.8.1

how can I handle this in Elasticsearch Config class

@Configuration
@ConfigurationProperties("es")
public class ESRestClient {

    private String hostName;
    private int port;
    private String username;
    private String password;

    @Bean
    public ElasticsearchClient getElasticSearchClient() {

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(username, password));

        RestClientBuilder builder = RestClient.builder(new HttpHost(hostName, port, "HTTPS"))
                .setHttpClientConfigCallback(
                        httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));

        // Create the low-level client
        RestClient restClient = builder.build();

        // Create the transport with a Jackson mapper
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());

        // And create the API client
        return new ElasticsearchClient(transport);
    }
}
                httpClientBuilder -> httpClientBuilder
                        .setDefaultCredentialsProvider(credentialsProvider)
                        .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                        .setMaxConnTotal(maxConnTotal)
                        .setMaxConnPerRoute(maxConnPerRoute));

I added the following code snippet to handle the connection pool

still getting the connection closed issue

I also recently observe this issue in production not sure why it happened but it was intermittent. Would like to know the root cause of this and how it can be avoided in future.

A ConnectionClosedException is raised by the http library when a connection has been closed unexpectedly.

This is likely caused by a network connectivity issue between the application and the Elasticsearch server.

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