# java.net.ConnectException while connection to cloud elasticsearch instance

**URL:** <https://discuss.elastic.co/t/java-net-connectexception-while-connection-to-cloud-elasticsearch-instance/170501>\
**Category:** Elasticsearch\
**Created:** [March 1, 2019, 1:06pm UTC](https://discuss.elastic.co/t/java-net-connectexception-while-connection-to-cloud-elasticsearch-instance/170501 "2019-03-01T13:06:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sanjay\_Das](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sanjay_das/32/41444_2.png) [@Sanjay\_Das](https://discuss.elastic.co/u/Sanjay_Das)\
**Post date:** [March 1, 2019, 1:06pm UTC](https://discuss.elastic.co/t/java-net-connectexception-while-connection-to-cloud-elasticsearch-instance/170501/1 "2019-03-01T13:06:17Z")

</div>

```
 private RestHighLevelClient createClientV2()
{
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USER, PASS));

    RestClientBuilder builder = RestClient.builder(new HttpHost(HOST, PORT, "https"))
            .setHttpClientConfigCallback(httpClientBuilder -> {
                httpClientBuilder.disableAuthCaching();
                return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            })
            .setMaxRetryTimeoutMillis(1000*60*5);

    return new RestHighLevelClient(builder);
}

private boolean executeRequestIndex(String indexName)
{
    boolean acknowledged = false;
    try {
        CreateIndexRequest request = new CreateIndexRequest(indexName);
        CreateIndexResponse createIndexResponse = elasticClient.indices().create(request, RequestOptions.DEFAULT);
        acknowledged = createIndexResponse.isAcknowledged();
    } catch (IOException e) {
        log.error(""+e.getMessage() + " :- " + e.getLocalizedMessage());
        e.printStackTrace();

    }
    return acknowledged;
}

```

**LOGS**  
java.net.ConnectException  
at org.elasticsearch.client.RestClient$SyncResponseListener.get(RestClient.java:949)  
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:229)  
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1593)  
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1563)  
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1525)  
at org.elasticsearch.client.IndicesClient.create(IndicesClient.java:146)  
at in.dproject.blog.dblog.service.ElaticService.executeRequestIndex(ElaticService.java:67)  
at in.dproject.blog.dblog.service.ElaticService.createIndex(ElaticService.java:53)  
at in.dproject.blog.dblog.controller.BLogController.createIndex(BLogController.java:22)  
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)  
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
at java.lang.reflect.Method.invoke(Method.java:498)  
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)  
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)  
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)  
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:849)  
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:760)  
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)  
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)  
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)  
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)  
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)  
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)  
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)  
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)  
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)  
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)  
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)  
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)  
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)  
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)  
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)  
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)  
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)  
at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:111)  
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)  
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)  
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

---

<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:** [March 1, 2019, 3:25pm UTC](https://discuss.elastic.co/t/java-net-connectexception-while-connection-to-cloud-elasticsearch-instance/170501/2 "2019-03-01T15:25:38Z")

</div>

Please don't post images of text as they are hardly readable and not searchable.

Instead paste the text and format it with `</>` icon. Check the preview window.

---

<div class="post-metadata">

**Author:** ![Sanjay\_Das](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sanjay_das/32/41444_2.png) [@Sanjay\_Das](https://discuss.elastic.co/u/Sanjay_Das)\
**Post date:** [March 2, 2019, 10:45am UTC](https://discuss.elastic.co/t/java-net-connectexception-while-connection-to-cloud-elasticsearch-instance/170501/3 "2019-03-02T10:45:53Z")

</div>

I changed the body , I am still struggling with this. if i use same host and cred in other REST tool it works but from code it throws ConnectException.

---

<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:** [March 2, 2019, 11:04am UTC](https://discuss.elastic.co/t/java-net-connectexception-while-connection-to-cloud-elasticsearch-instance/170501/4 "2019-03-02T11:04:25Z")

</div>

Please make your code looking like this:

```auto
public RestHighLevelClient createClientV2() {
  final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  // ...
}

```

That's definitely easier to read...

So please format your code, logs or configuration files using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) and not the citation button. It will make your post more readable.

Or use markdown style like:

````
```
CODE
```

````

This is the icon to use if you are not using markdown format:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/e/7e6e239431ec2d71cbf1beef741f2e93e7cc762c.jpg)

There's a live preview panel for exactly this reasons.

Anyway, here is some code that might help. It's using a 6.6 client but that should be similar:

> <https://github.com/dadoonet/fscrawler/blob/master/elasticsearch-client/elasticsearch-client-v6/src/main/java/fr/pilato/elasticsearch/crawler/fs/client/v6/ElasticsearchClientV6.java#L406-L420>

HTH

---

<div class="post-metadata">

**Author:** ![Sanjay\_Das](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sanjay_das/32/41444_2.png) [@Sanjay\_Das](https://discuss.elastic.co/u/Sanjay_Das)\
**Post date:** [March 2, 2019, 11:56am UTC](https://discuss.elastic.co/t/java-net-connectexception-while-connection-to-cloud-elasticsearch-instance/170501/5 "2019-03-02T11:56:33Z")

</div>

Hi I pasted code as per your suggesstion. Mean while I was debbuging the issue , I found, this is because my Java client is running under proxy. If I run the code without proxy connection then the code is able to create index in the cloud elasticsearch.

Now my Question is how to work with this Java client under proxy internet to work with cloud ??

---

<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:** [March 2, 2019, 12:17pm UTC](https://discuss.elastic.co/t/java-net-connectexception-while-connection-to-cloud-elasticsearch-instance/170501/6 "2019-03-02T12:17:34Z")

</div>

> [@Sanjay\_Das](#):
>
> Hi I pasted code as per your suggesstion.

Great. Next time please do it for the logs as well.

> [@Sanjay\_Das](#):
>
> Now my Question is how to work with this Java client under proxy internet to work with cloud ??

Did you see [https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low-usage-initialization.html](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low-usage-initialization.html)?

```auto
RestClientBuilder builder = RestClient.builder(
    new HttpHost("localhost", 9200, "http"));
builder.setHttpClientConfigCallback(new HttpClientConfigCallback() {
        @Override
        public HttpAsyncClientBuilder customizeHttpClient(
                HttpAsyncClientBuilder httpClientBuilder) {
            return httpClientBuilder.setProxy(
                new HttpHost("proxy", 9000, "http"));  
        }
    });

```

---

<div class="post-metadata">

**Author:** ![Sanjay\_Das](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sanjay_das/32/41444_2.png) [@Sanjay\_Das](https://discuss.elastic.co/u/Sanjay_Das)\
**Post date:** [March 4, 2019, 5:11am UTC](https://discuss.elastic.co/t/java-net-connectexception-while-connection-to-cloud-elasticsearch-instance/170501/7 "2019-03-04T05:11:39Z")

</div>

Hi [@dadoonet](https://twitter.com/dadoonet) thanks for solving this problem. After adding Proxy host , the issue is solved. 😄

---

<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:** [April 1, 2019, 5:11am UTC](https://discuss.elastic.co/t/java-net-connectexception-while-connection-to-cloud-elasticsearch-instance/170501/8 "2019-04-01T05:11:40Z")

</div>

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