# The IOException occurred while using HttpClient connect Elasticsearch

**URL:** <https://discuss.elastic.co/t/the-ioexception-occurred-while-using-httpclient-connect-elasticsearch/55827>\
**Category:** Elasticsearch\
**Created:** [July 19, 2016, 6:47am UTC](https://discuss.elastic.co/t/the-ioexception-occurred-while-using-httpclient-connect-elasticsearch/55827 "2016-07-19T06:47:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rekadowney](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rekadowney/32/106313_2.png) [@rekadowney](https://discuss.elastic.co/u/rekadowney)\
**Post date:** [July 19, 2016, 6:47am UTC](https://discuss.elastic.co/t/the-ioexception-occurred-while-using-httpclient-connect-elasticsearch/55827/1 "2016-07-19T06:47:10Z")

</div>

I used the `HttpClient` to connect `Elasticsearch`. The code likes these,

```
@Test
public void queryIndices() throws IOException {

    // new a get request
    HttpGet request = new HttpGet("http://localhost:9200/_cat/indices?pretty");

    // config the timeout
    RequestConfig config = RequestConfig.custom()
            .setSocketTimeout(2000)
            .setConnectTimeout(2000)
            .setConnectionRequestTimeout(2000)
            .build();
    request.setConfig(config);

    // construct a httpClient and execute the get request
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = httpClient.execute(request);

    // get the response message
    HttpEntity entity = response.getEntity();
    String responseText = EntityUtils.toString(entity, StandardCharsets.UTF_8);
    System.out.println(responseText);

    // close the response
    EntityUtils.consume(entity);
    response.close();
    
}

```

While this Unit Test has ran and terminated, the `Elasticsearch` thrown such a excpetion,

> [2016-07-19 14:30:48,329][WARN][http.netty] [Sin] Caught exception while handling client http traffic, closing connection [id: 0x9940a6e4, /127.0.0.1:65380 =\> /127.0.0.1:9200]  
> java.io.IOException: An existing connection was forcibly closed by the remote host  
> at sun.nio.ch.SocketDispatcher.read0(Native Method)  
> at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)  
> at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)  
> at sun.nio.ch.IOUtil.read(IOUtil.java:192)  
> at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)  
> at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:64)  
> at org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108)  
> at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:337)  
> at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89)  
> at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178)  
> at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)  
> at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)  
> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)  
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)  
> at java.lang.Thread.run(Thread.java:745)

Howerver, the result of Unit Test is **green** , **I want to know will it has effect on the Elasticsearch and can it be fixed.**

**Request your kind help!**  
**Thanks!**

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [July 19, 2016, 11:16am UTC](https://discuss.elastic.co/t/the-ioexception-occurred-while-using-httpclient-connect-elasticsearch/55827/2 "2016-07-19T11:16:31Z")

</div>

Close the `httpClient`. I'd use try-with-resources for everything with the word "closeable" in the name.

2 seconds is a bit low for aggregations, btw. If you are going to set a timeone on the connection like that you should also set it on the Elasticsearch request so Elasticsearch can make an effort to timeout the request internally.

---

<div class="post-metadata">

**Author:** ![rekadowney](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rekadowney/32/106313_2.png) [@rekadowney](https://discuss.elastic.co/u/rekadowney)\
**Post date:** [July 20, 2016, 12:56am UTC](https://discuss.elastic.co/t/the-ioexception-occurred-while-using-httpclient-connect-elasticsearch/55827/3 "2016-07-20T00:56:10Z")

</div>

**Thanks for your help!**  
It is my mistake that forget close the `httpClient`, and in the actual environment, it will run until the app closed.  
I will configure the `Elasticsearch`'s timeout and adjust the timeout to the "aggregation operation" if i use.

---

<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 5, 2017, 10:34pm UTC](https://discuss.elastic.co/t/the-ioexception-occurred-while-using-httpclient-connect-elasticsearch/55827/4 "2017-07-05T22:34:15Z")

</div>


