# Providing Socket tineout exception not working in elastic java api

**URL:** <https://discuss.elastic.co/t/providing-socket-tineout-exception-not-working-in-elastic-java-api/350923>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [January 12, 2024, 9:15am UTC](https://discuss.elastic.co/t/providing-socket-tineout-exception-not-working-in-elastic-java-api/350923 "2024-01-12T09:15:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Divy\_Garg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/divy_garg/32/79288_2.png) [@Divy\_Garg](https://discuss.elastic.co/u/Divy_Garg)\
**Post date:** [January 12, 2024, 9:15am UTC](https://discuss.elastic.co/t/providing-socket-tineout-exception-not-working-in-elastic-java-api/350923/1 "2024-01-12T09:15:58Z")

</div>

Hi I am using below libraries to connect to elasticsearch from java  
implementation group: 'co.elastic.clients', name: 'elasticsearch-java', version: '8.11.4'  
implementation group: 'org.elasticsearch.client', name: 'elasticsearch-rest-client', version: '8.11.4'

I need to provide timeout in my queries to cancel the running query after 50-60 ms as i need to provide response in given time frame.

I explored .timeout() in queries but it didn't work and in my knowledge it was working on per shard level and not on client level.

Then i found we can pass socket timeout to terminate the queries in low level client. This I am using as :

```auto

RestClient restClient = RestClient.builder(
                new HttpHost(esHost, Integer.parseInt(esPort)))
                    .setRequestConfigCallback(
                        requestConfigBuilder -> requestConfigBuilder
                                .setConnectTimeout(1)
                                .setSocketTimeout(1))
                .build();

 ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());

 return new ElasticsearchClient(transport);

```

My search query looks like this :

```auto
SearchResponse<Void> response = elasticClient.search(s -> s
                        .index(index)
                        .size(0)
                        .timeout("1ms")
                        .aggregations(map)
                        .query(q -> q
                                .bool(b -> b.should(matchEntityQueries))
                        ),
                Void.class
        );

```

But as I am testing this it is also not working. As you can see i have given **1 ms** in both timeout values but request doesn't timeout and keeps on running for response. Please suggest me what I am missing as I have read tonnes of articles on this but anything doesn't seems to explain this.

Thanks

---

<div class="post-metadata">

**Author:** ![Divy\_Garg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/divy_garg/32/79288_2.png) [@Divy\_Garg](https://discuss.elastic.co/u/Divy_Garg)\
**Post date:** [January 12, 2024, 9:16am UTC](https://discuss.elastic.co/t/providing-socket-tineout-exception-not-working-in-elastic-java-api/350923/2 "2024-01-12T09:16:53Z")

</div>

@dadoonet Can you please help here?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 12, 2024, 11:52am UTC](https://discuss.elastic.co/t/providing-socket-tineout-exception-not-working-in-elastic-java-api/350923/3 "2024-01-12T11:52:55Z")

</div>

Please be patient and refrain from pinging people like this unless they are already discussing in your thread.

May I suggest that you switch to the async client instead? I wrote an example here which seems to be doing what you would expect.

> <https://github.com/dadoonet/elasticsearch-java-client-demo/blob/c67633f85dea04f7b26424076dc0b638e8443dee/src/test/java/fr/pilato/test/elasticsearch/hlclient/EsClientIT.java#L541-L569>

---

<div class="post-metadata">

**Author:** ![ltrotta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ltrotta/32/124003_2.png) [@ltrotta](https://discuss.elastic.co/u/ltrotta)\
**Post date:** [January 12, 2024, 1:02pm UTC](https://discuss.elastic.co/t/providing-socket-tineout-exception-not-working-in-elastic-java-api/350923/4 "2024-01-12T13:02:45Z")

</div>

Hey!

Just to add some context:  
Yes the `.timeout()` query parameter works per shard, so except for elaborate queries on complex indexes it will rarely exceed `1ms` (which is the minimum value).

---

<div class="post-metadata">

**Author:** ![Divy\_Garg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/divy_garg/32/79288_2.png) [@Divy\_Garg](https://discuss.elastic.co/u/Divy_Garg)\
**Post date:** [January 12, 2024, 1:06pm UTC](https://discuss.elastic.co/t/providing-socket-tineout-exception-not-working-in-elastic-java-api/350923/5 "2024-01-12T13:06:44Z")

</div>

@dadoonet Apologies for directly tagging you.  
Thanks, the code is working properly using async client and is giving timeout as intended.  
But Is there any way we can achieve this using synchronous client and why  
RequestConfigCallback is not working as expected even when we set socketTimeout. It would be really helpful if you can explain on this?

---

<div class="post-metadata">

**Author:** ![Divy\_Garg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/divy_garg/32/79288_2.png) [@Divy\_Garg](https://discuss.elastic.co/u/Divy_Garg)\
**Post date:** [January 12, 2024, 1:09pm UTC](https://discuss.elastic.co/t/providing-socket-tineout-exception-not-working-in-elastic-java-api/350923/6 "2024-01-12T13:09:16Z")

</div>

@ltrotta Yes had to go through various docs to understand that it is for per shard timeout. Thank you

---

<div class="post-metadata">

**Author:** ![ltrotta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ltrotta/32/124003_2.png) [@ltrotta](https://discuss.elastic.co/u/ltrotta)\
**Post date:** [January 12, 2024, 3:07pm UTC](https://discuss.elastic.co/t/providing-socket-tineout-exception-not-working-in-elastic-java-api/350923/7 "2024-01-12T15:07:22Z")

</div>

It's strange that RequestConfigCallback is not working here, I have just tested it with those same libraries and configurations and it returns  
`java.net.SocketTimeoutException: 1 milliseconds timeout on connection`  
as expected.  
If this is still an issue could you provide more information so that we can try to reproduce the problem?

---

<div class="post-metadata">

**Author:** ![Divy\_Garg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/divy_garg/32/79288_2.png) [@Divy\_Garg](https://discuss.elastic.co/u/Divy_Garg)\
**Post date:** [January 15, 2024, 3:56pm UTC](https://discuss.elastic.co/t/providing-socket-tineout-exception-not-working-in-elastic-java-api/350923/8 "2024-01-15T15:56:29Z")

</div>

Thanks for taking time to solve this. But for sync client I am using it in basic way as provided earlier. Not sure what is causing the issue here. Anyways async client works for me for now. Will get back to it if needed in future.  
Thanks 🙂

---

<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 12, 2024, 3:56pm UTC](https://discuss.elastic.co/t/providing-socket-tineout-exception-not-working-in-elastic-java-api/350923/9 "2024-02-12T15:56:51Z")

</div>

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