# Getting this error - java.util.concurrent.CancellationException: Request execution cancelled

**URL:** <https://discuss.elastic.co/t/getting-this-error-java-util-concurrent-cancellationexception-request-execution-cancelled/350579>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [January 8, 2024, 12:29pm UTC](https://discuss.elastic.co/t/getting-this-error-java-util-concurrent-cancellationexception-request-execution-cancelled/350579 "2024-01-08T12:29:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr.fantastic](https://avatars.discourse-cdn.com/v4/letter/m/3ec8ea/32.png) [@mr.fantastic](https://discuss.elastic.co/u/mr.fantastic)\
**Post date:** [January 8, 2024, 12:29pm UTC](https://discuss.elastic.co/t/getting-this-error-java-util-concurrent-cancellationexception-request-execution-cancelled/350579/1 "2024-01-08T12:29:25Z")

</div>

I am facing this issue, where my app tried to send search requests to es and encounters this issue.  
my client config is -

```auto
> RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(elasticSearchProps.getHost(),
> elasticSearchProps.getPort(), elasticSearchProps.getScheme()));
> 
> restClientBuilder.setCompressionEnabled(true);
> 
>     
> if (elasticSearchProps.getAuthenticationRequired()) {
> final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
> 
> credentialsProvider.setCredentials(AuthScope.ANY,
> new UsernamePasswordCredentials(elasticSearchProps.getUsername(), elasticSearchProps.getPassword()));
> 
> restClientBuilder.setHttpClientConfigCallback(
> httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
> }
> 
> ElasticsearchTransport transport = new RestClientTransport(restClientBuilder.build(), new JacksonJsonpMapper());
> 
> RequestConfig requestConfig = RequestConfig.DEFAULT;
> RequestOptions.Builder requestOptionsBuilder = RequestOptions.DEFAULT
> .toBuilder()
> .setRequestConfig(requestConfig);
> 
> requestOptionsBuilder.setHttpAsyncResponseConsumerFactory(
> new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory(
> 104857600
> )
> );
> 
> return new ElasticsearchClient(transport, new RestClientOptions(requestOptionsBuilder.build()));

```

The no. of request being made is considerably high.  
How ever there are other search operation, delete, index operations that are working but I am getting this error from a method where i am search the index with track total hits as true to get total count of the doc matching the query.  
Am i doing something wrong here?

implementation("co.elastic.clients:elasticsearch-java:8.5.2")  
elastic clustor version - 8.5.2

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [January 8, 2024, 4:10pm UTC](https://discuss.elastic.co/t/getting-this-error-java-util-concurrent-cancellationexception-request-execution-cancelled/350579/2 "2024-01-08T16:10:53Z")

</div>

This exception happens when the http client encountered an `Error` (and not only an `Exception`) when calling a response listener. A frequent cause for this is an `OutOfMemoryError` happening in these listeners.

The upcoming version 8.12 contains a protection against this that reports such errors to the application [using a custom response consumer factory](https://github.com/elastic/elasticsearch-java/blob/main/java-client/src/main/java/co/elastic/clients/transport/rest_client/SafeResponseConsumer.java). The [documentation explains how to use it with previous versions](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/8.12/io-reactor-errors.html) of the Java API client.

---

<div class="post-metadata">

**Author:** ![mr.fantastic](https://avatars.discourse-cdn.com/v4/letter/m/3ec8ea/32.png) [@mr.fantastic](https://discuss.elastic.co/u/mr.fantastic)\
**Post date:** [January 9, 2024, 7:09am UTC](https://discuss.elastic.co/t/getting-this-error-java-util-concurrent-cancellationexception-request-execution-cancelled/350579/3 "2024-01-09T07:09:58Z")

</div>

I added the SafeConsumerResponse file in my project and configured the transport to use the SafeConsumerRepsonse.Default\_Options as mentioned in the document.

I still see the same error in the logs on aplication startup.  
You mentioned this issue can be caused by OutOfMemoryError, but i dont see any logs indication this. Could the error be because of something else?  
Adding some stack trace where I am getting the Request execution cancelled.

```auto
extendedStackTrace":
"java.util.concurrent.CancellationException: Request execution cancelled\n\tat 
org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase.execute(CloseableHttpAsyncClientBase.java:114) ~[httpasyncclient-4.1.5.jar:4.1.5]\n\tat 
org.apache.http.impl.nio.client.InternalHttpAsyncClient.execute(InternalHttpAsyncClient.java:138) ~[httpasyncclient-4.1.5.jar:4.1.5]\n\tat 
org.elasticsearch.client.RestClient.performRequest(RestClient.java:296) ~[elasticsearch-rest-client-8.5.2.jar:8.5.2]\n\tat 
org.elasticsearch.client.RestClient.performRequest(RestClient.java:288) ~[elasticsearch-rest-client-8.5.2.jar:8.5.2]\n\tat 
co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:147) ~[elasticsearch-java-8.5.2.jar:?]\n\tat 
co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1518) ~[elasticsearch-java-8.5.2.jar:?]\n\tat

```

new tranport confid in es bean -

```auto
    RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(elasticSearchProps.getHost(),
            elasticSearchProps.getPort(), elasticSearchProps.getScheme()));

    restClientBuilder.setCompressionEnabled(true);

    // Add credentials provider if auth is required
    if (elasticSearchProps.getAuthenticationRequired()) {
      final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

      credentialsProvider.setCredentials(AuthScope.ANY,
              new UsernamePasswordCredentials(elasticSearchProps.getUsername(), elasticSearchProps.getPassword()));

      restClientBuilder.setHttpClientConfigCallback(
              httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
    }

    RestClientOptions options = new RestClientOptions(
            SafeResponseConsumer.DEFAULT_REQUEST_OPTIONS
    );
    RestClientTransport transport = new RestClientTransport(
            restClientBuilder.build(), new JacksonJsonpMapper(), options
    );

// ElasticsearchTransport transport = new RestClientTransport(restClientBuilder.build(), new JacksonJsonpMapper());

    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectionRequestTimeout(elasticSearchProps.getConnectionTimeOut())
            .setSocketTimeout(elasticSearchProps.getSocketTimeOut())
            .build();

    RequestOptions.Builder requestOptionsBuilder = RequestOptions.DEFAULT
            .toBuilder()
            .setRequestConfig(requestConfig);

    requestOptionsBuilder.setHttpAsyncResponseConsumerFactory(
            new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory(
                    104857600
            )
    );

    return new ElasticsearchClient(transport, new RestClientOptions(requestOptionsBuilder.build()));

```

---

<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 6, 2024, 7:10am UTC](https://discuss.elastic.co/t/getting-this-error-java-util-concurrent-cancellationexception-request-execution-cancelled/350579/4 "2024-02-06T07:10:46Z")

</div>

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