# Recommended HighLevelRestClient configuration for Spring App

**URL:** <https://discuss.elastic.co/t/recommended-highlevelrestclient-configuration-for-spring-app/140427>\
**Category:** Elasticsearch\
**Created:** [July 18, 2018, 3:08am UTC](https://discuss.elastic.co/t/recommended-highlevelrestclient-configuration-for-spring-app/140427 "2018-07-18T03:08:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kishoreji](https://avatars.discourse-cdn.com/v4/letter/k/e495f1/32.png) [@kishoreji](https://discuss.elastic.co/u/kishoreji)\
**Post date:** [July 18, 2018, 3:08am UTC](https://discuss.elastic.co/t/recommended-highlevelrestclient-configuration-for-spring-app/140427/1 "2018-07-18T03:08:58Z")

</div>

Hi,

Is it a good practice to create HighLevelRestClient as a bean in Spring config and autowire it wherever its needed? like below:

/\*\* Configuration Bean \*/  
@Configuration  
public class ElasticsearchConfig {

```
@Value("${elasticsearch.host}")
private String host;

@Value("${elasticsearch.port}")
private int port;

@Bean
public HighLevelRestClient restClient() {
    return RestClient.builder(new HttpHost(host, port)).build();
}

```

}

// use the dependency in your other components/services, using dependency injection  
@Autowired  
private RestClient restClient;

I am asking this because I am getting Timeouts, if the app is idle for a period & did not make connections to the Elastic Hosts. The subsequent requests goes smoothly.

---

<div class="post-metadata">

**Author:** ![kishoreji](https://avatars.discourse-cdn.com/v4/letter/k/e495f1/32.png) [@kishoreji](https://discuss.elastic.co/u/kishoreji)\
**Post date:** [July 23, 2018, 5:32am UTC](https://discuss.elastic.co/t/recommended-highlevelrestclient-configuration-for-spring-app/140427/2 "2018-07-23T05:32:55Z")

</div>

More details on the issue:

Issue: I was making the RestClient as a Bean managed by Spring container at starup and hoping that all the subsequent request to the app will be able to utilize it. It works fine too, but only until a pause or idle time. What I observed is - if there is a idle time for 20-25 mins the RestClient is loosing the stability. So when the next request comes, the very first times out !!  
After that one request, Rest Client comes back into the business and everything goes back to normal.

I may have missed something here but couldn't find how RestClient will continue to maintain the connect with the given hosts? I see documents around Sniffer doing the hearbeats checks though.

So, I am going back to create a fresh rest client for each request coming to my server, until I get something concrete on this. 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:** [August 20, 2018, 5:38am UTC](https://discuss.elastic.co/t/recommended-highlevelrestclient-configuration-for-spring-app/140427/3 "2018-08-20T05:38:39Z")

</div>

Closed on August 20, 2018, 5:38am UTC

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