# Close Channel Exceptions while using transport client with bulkprocessor

**URL:** <https://discuss.elastic.co/t/close-channel-exceptions-while-using-transport-client-with-bulkprocessor/124490>\
**Category:** Elasticsearch\
**Created:** [March 19, 2018, 6:48am UTC](https://discuss.elastic.co/t/close-channel-exceptions-while-using-transport-client-with-bulkprocessor/124490 "2018-03-19T06:48:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kumar1993](https://avatars.discourse-cdn.com/v4/letter/k/8baadc/32.png) [@kumar1993](https://discuss.elastic.co/u/kumar1993)\
**Post date:** [March 19, 2018, 6:48am UTC](https://discuss.elastic.co/t/close-channel-exceptions-while-using-transport-client-with-bulkprocessor/124490/1 "2018-03-19T06:48:35Z")

</div>

I have a Java SE application which is using transport client and high level rest client for indexing data into elasticsearch, I am using a bulk processor to process the requests (index,delete,update) in bulk, like this

```
 processor.add(client.prepareIndex(index, type).setSource(s).request());

```

The bulk processor is getting created like

```
BulkProcessor
    .builder(client, initBulkProcessorListener())
    .setBulkActions(Integer.valueOf(getEsProperty("elasticsearch.bulkrequestsize")))
    .setBulkSize(new ByteSizeValue(5, ByteSizeUnit.MB))
    .setFlushInterval(TimeValue.timeValueSeconds(5))
    .setConcurrentRequests(1)
    .setBackoffPolicy(BackoffPolicy.exponentialBackoff())
    .build();

```

i do a flush of pending requests using the following method

```
public void flushPendingRequests(BulkProcessor processor) {
 processor.flush();
 try {
  processor.awaitClose(10000, TimeUnit.MILLISECONDS);
 } catch (InterruptedException e) {
  LOGGER.error("Interrupted Exception : " + e.getMessage());
  Thread.currentThread().interrupt();
 }
 processor.close();
 }

```

later on i am closing the client instance using the following method

```
  public void close() {
LOGGER.info("Closing transport client");
if (client == null) {
  LOGGER.info("Transport client already closed");
  return;
}

// This is to prevent closed channel exceptions
shutdownThreadPool(client.threadPool().scheduler());
try {
  Thread.sleep(1000);
} catch (InterruptedException e) {
  e.printStackTrace();
}
client.close();

```

}

```
private static void shutdownThreadPool(ScheduledExecutorService pool) {
pool.shutdown();
try {
  boolean terminated = pool.awaitTermination(THREADPOOL_TIMEOUT, THREADPOOL_TIMEOUT_UNIT);
  if (!terminated) {
    LOGGER.warn("Thread pool timeout elapsed before termination, wait again for " + THREADPOOL_TIMEOUT + " "
        + THREADPOOL_TIMEOUT_UNIT.toString() + "...");
    pool.shutdownNow();
    if (!pool.awaitTermination(THREADPOOL_TIMEOUT, THREADPOOL_TIMEOUT_UNIT)) {
      LOGGER.error("Thread pool did not terminate");
    }
  }
} catch (InterruptedException e) {
  LOGGER.error("Error occurred while shutting down the thread pool: " + e.getMessage(), e);
  pool.shutdownNow();
  Thread.currentThread().interrupt();
}
}

```

Even though everything seems to be working fine, i always get closeChannel Exceptions in elasticsearch logs when the application shuts down, it disappears when i add a Thread.sleep(1000); though, i don't seem to understand what the problem is exactly, help is much appreciated.  
Cheers!

---

<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 16, 2018, 6:48am UTC](https://discuss.elastic.co/t/close-channel-exceptions-while-using-transport-client-with-bulkprocessor/124490/2 "2018-04-16T06:48:40Z")

</div>

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