# java.net.ConnectException: Connection Refused

**URL:** <https://discuss.elastic.co/t/java-net-connectexception-connection-refused/135803>\
**Category:** Elasticsearch\
**Created:** [June 13, 2018, 10:45pm UTC](https://discuss.elastic.co/t/java-net-connectexception-connection-refused/135803 "2018-06-13T22:45:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![anna10](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@anna10](https://discuss.elastic.co/u/anna10)\
**Post date:** [June 13, 2018, 10:45pm UTC](https://discuss.elastic.co/t/java-net-connectexception-connection-refused/135803/1 "2018-06-13T22:45:28Z")

</div>

I am using the Java HighLevelRestClient to connect to my elastic instance hosted on AWS. I can make requests against the URL on postman and from my browser just fine, but when I use the client library I get a java.net.ConnectException: Connection Refused. (I don't currently need any authentication as this is a small public test instance).

Edit: I have set up the ElasticSearch [6.2] instance on the AWS console and am making requests against the domain that is created. I don't see any ports that are configured for my domain.

This is my code:

```auto
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);

        GetRequest getRequest = new GetRequest("some_index", "some_type","some_id");
        final String[] elasticGetResponse = new String[1];

        restHighLevelClient.getAsync(getRequest, new ActionListener<GetResponse>() {

            @Override
            public void onResponse(GetResponse documentFields) {
                try {
                    elasticGetResponse[0] = restHighLevelClient.get(getRequest).toString();
                }

                catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Exception e) {
                e.printStackTrace();
            }
        });

```

I'm not sure what I'm doing incorrectly. All help is appreciated!

Update: I built the restClientBuilder with the following

```auto

        MySSLHelper sslHelper = new MySSLHelper(SSLConfig.builder()
                .withKeyStoreProvider(myKeyStoreProvider)
                .withTrustStoreProvider(InternalTrustStoreProvider.INSTANCE)
                .build());

        RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost("MY_ELASTICSEARCH_ENDPOINT")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
                return httpAsyncClientBuilder.setSSLContext(sslHelper.getContext());
            }
        });

```

---

<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:** [June 14, 2018, 2:19am UTC](https://discuss.elastic.co/t/java-net-connectexception-connection-refused/135803/2 "2018-06-14T02:19:17Z")

</div>

How did you create the `restClientBuilder` object?

---

<div class="post-metadata">

**Author:** ![anna10](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@anna10](https://discuss.elastic.co/u/anna10)\
**Post date:** [June 14, 2018, 3:44am UTC](https://discuss.elastic.co/t/java-net-connectexception-connection-refused/135803/3 "2018-06-14T03:44:24Z")

</div>

updated the original post with the code.

---

<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:** [June 14, 2018, 5:36am UTC](https://discuss.elastic.co/t/java-net-connectexception-connection-refused/135803/4 "2018-06-14T05:36:40Z")

</div>

You said there is no auth.

Are you using https here?

---

<div class="post-metadata">

**Author:** ![anna10](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@anna10](https://discuss.elastic.co/u/anna10)\
**Post date:** [June 14, 2018, 2:04pm UTC](https://discuss.elastic.co/t/java-net-connectexception-connection-refused/135803/5 "2018-06-14T14:04:39Z")

</div>

I'm not sure about this, I don't think so. My access policy is the public template.

---

<div class="post-metadata">

**Author:** ![anna10](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@anna10](https://discuss.elastic.co/u/anna10)\
**Post date:** [June 14, 2018, 3:18pm UTC](https://discuss.elastic.co/t/java-net-connectexception-connection-refused/135803/6 "2018-06-14T15:18:29Z")

</div>

Hey David, on the latest version [6.2] do I need to host the elasticsearch instance locally and use the AWS CLI to access and deploy it? Currently I have only set up the instance via the AWS Console and am trying to hit endpoints at the endpoint provided.

---

<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:** [June 14, 2018, 3:38pm UTC](https://discuss.elastic.co/t/java-net-connectexception-connection-refused/135803/7 "2018-06-14T15:38:33Z")

</div>

Are you running Elasticsearch as as service by AWS by any chance?

Can you just curl the instance and if so on which address?

---

<div class="post-metadata">

**Author:** ![anna10](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@anna10](https://discuss.elastic.co/u/anna10)\
**Post date:** [June 14, 2018, 3:40pm UTC](https://discuss.elastic.co/t/java-net-connectexception-connection-refused/135803/8 "2018-06-14T15:40:45Z")

</div>

Yes I am. I can curl the instance but I want to programmatically access and populate the instance using the Java Client.

I will send you a direct message with the URL.

---

<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:** [June 14, 2018, 4:01pm UTC](https://discuss.elastic.co/t/java-net-connectexception-connection-refused/135803/9 "2018-06-14T16:01:08Z")

</div>

Few comments:

1st: never ever expose elasticsearch on internet without any security like X-Pack.  
2nd: I can't help because I don't know their service so you should ask your AWS support  
3rd: Did you look at [https://www.elastic.co/cloud](https://www.elastic.co/cloud) and [https://aws.amazon.com/marketplace/pp/B01N6YCISK](https://aws.amazon.com/marketplace/pp/B01N6YCISK) ?

Cloud by elastic is the only way to have access to X-Pack. Think about what is there yet like Security, Monitoring, Reporting and what is coming like Canvas, SQL...

It also runs on AWS or GCP. And 6.3.0 is already available there!

I can totally help on [cloud.elastic.co](http://cloud.elastic.co) service although you have a support team for you as well.

---

<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:** [July 12, 2018, 4:08pm UTC](https://discuss.elastic.co/t/java-net-connectexception-connection-refused/135803/10 "2018-07-12T16:08:32Z")

</div>

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