# Java ElasticsearchClient ping timeout

**URL:** <https://discuss.elastic.co/t/java-elasticsearchclient-ping-timeout/322749>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [January 9, 2023, 4:15pm UTC](https://discuss.elastic.co/t/java-elasticsearchclient-ping-timeout/322749 "2023-01-09T16:15:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ewolfman](https://avatars.discourse-cdn.com/v4/letter/e/5f8ce5/32.png) [@ewolfman](https://discuss.elastic.co/u/ewolfman)\
**Post date:** [January 9, 2023, 4:15pm UTC](https://discuss.elastic.co/t/java-elasticsearchclient-ping-timeout/322749/1 "2023-01-09T16:15:50Z")

</div>

Hi,

We are caching the created connection, and use ping often to ensure that the connection is still valid and would like it to fail quickly if not so we can create a new one.

With the Java High Level REST Client we can provide RequestOptions allowing us to set a short timeout for the ping.

```auto
boolean response = client.ping(myPingRequestOptions);

```

This was very helpful as you could have different timeouts for searches and pings.

With the new Java API Client it looks like you cannot pass such custom request options and only set timeouts when creating the connection:

> **[ElasticsearchClient (java-client 8.5.3 API)](https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.5.3/co/elastic/clients/elasticsearch/ElasticsearchClient.html#ping())**
>
> declaration: package: co.elastic.clients.elasticsearch, class: ElasticsearchClient

Is there anyway to reuse the **same** connection but set a different timeout for the ping?

Thanks

---

<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 9, 2023, 4:43pm UTC](https://discuss.elastic.co/t/java-elasticsearchclient-ping-timeout/322749/2 "2023-01-09T16:43:24Z")

</div>

This isn't possible at the moment. However there is a workaround: `ping` is a very simple API endpoint and it's easy to call it with the low level REST client used to create the Java API client:

```java
RequestOptions options = RequestOptions.DEFAULT.toBuilder()
	.setRequestConfig(RequestConfig.custom().setConnectionRequestTimeout(1000).build())
	.build();
Request request = new Request("HEAD", "/");
request.setOptions(options);
restClient.performRequest(request);

```

However I'm wondering why the connection would become invalid.

---

<div class="post-metadata">

**Author:** ![ewolfman](https://avatars.discourse-cdn.com/v4/letter/e/5f8ce5/32.png) [@ewolfman](https://discuss.elastic.co/u/ewolfman)\
**Post date:** [January 9, 2023, 5:51pm UTC](https://discuss.elastic.co/t/java-elasticsearchclient-ping-timeout/322749/3 "2023-01-09T17:51:11Z")

</div>

> [@swallez](#):
>
> ```auto
> RequestOptions options = RequestOptions.DEFAULT.toBuilder()
> .setRequestConfig(RequestConfig.custom().setConnectionRequestTimeout(1000).build())
> .build();
> Request request = new Request("HEAD", "/");
> request.setOptions(options);
> restClient.performRequest(request);
> 
> ```

Thanks.

Unfortunately the connection sometimes drops, terminates etc.

---

<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, 2023, 5:51pm UTC](https://discuss.elastic.co/t/java-elasticsearchclient-ping-timeout/322749/4 "2023-02-06T17:51:43Z")

</div>

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