# Rest High Level client - Gracefully flushing bulk processor as JVM exits

**URL:** https://discuss.elastic.co/t/rest-high-level-client-gracefully-flushing-bulk-processor-as-jvm-exits/217368
**Category:** Elasticsearch
**Created:** [January 31, 2020, 12:33pm UTC](https://discuss.elastic.co/t/rest-high-level-client-gracefully-flushing-bulk-processor-as-jvm-exits/217368 "2020-01-31T12:33:38Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![marchuss](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@marchuss](https://discuss.elastic.co/u/marchuss)
#### Post date: [January 31, 2020, 12:33pm UTC](https://discuss.elastic.co/t/rest-high-level-client-gracefully-flushing-bulk-processor-as-jvm-exits/217368/1 "2020-01-31T12:33:38Z")

</div>

Are there any recommendations on how to cleanly flush all items that are in the Rest High Level bulkprocessor (configured with one concurrent request) as the JVM exits? We have a spring boot application and have tried to manually flush the bulkprocessor as the bean is destroyed and also the awaitClose, but in both cases the bulkprocessor does not seem to be flushing the items out to ES and the bulk items are lost.

This is using Rest High level client 7.5.1 and Elastic Search version 7.4.0

Many thanks

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [January 31, 2020, 1:26pm UTC](https://discuss.elastic.co/t/rest-high-level-client-gracefully-flushing-bulk-processor-as-jvm-exits/217368/2 "2020-01-31T13:26:49Z")

</div>

Hey,

closing the bulkprocessor should flush out remaining items, see [https://github.com/elastic/elasticsearch/blob/7.5/server/src/main/java/org/elasticsearch/action/bulk/BulkProcessor.java#L329-L331](https://github.com/elastic/elasticsearch/blob/7.5/server/src/main/java/org/elasticsearch/action/bulk/BulkProcessor.java#L329-L331)

However please take a look at the javadocs, if concurrent requests are enabled, you need to specify the amount of time you want to wait. Also, what is the return value of `awaitClose()` when you call it?

Maye you can also mention how the bulk processor is configured.

---

<div class="post-metadata">

### Author: ![marchuss](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@marchuss](https://discuss.elastic.co/u/marchuss)
#### Post date: [January 31, 2020, 2:08pm UTC](https://discuss.elastic.co/t/rest-high-level-client-gracefully-flushing-bulk-processor-as-jvm-exits/217368/3 "2020-01-31T14:08:57Z")

</div>

Thanks for the reply. We currently have the bulkprocessor configured as follows:

BulkProcessor.builder(consumer, listenerr)  
.setBulkActions(1000)  
.setBulkSize(new ByteSizeValue(5, ByteSizeUnit.MB))  
.setFlushInterval(TimeValue.timeValueSeconds(5))  
.setConcurrentRequests(1)  
.setBackoffPolicy(BackoffPolicy.exponentialBackoff(TimeValue.timeValueMillis(100), 3))  
.build();  
When we explicitly call awaitClose it returns immediately in the dispose method and we'll try and get the return value for more information. One thing we did notice is that the afterBulk method Throwable contains the Interrupted Exception (which I am guessing is expected as the JVM shuts down).

Thanks

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [February 3, 2020, 3:05pm UTC](https://discuss.elastic.co/t/rest-high-level-client-gracefully-flushing-bulk-processor-as-jvm-exits/217368/4 "2020-02-03T15:05:36Z")

</div>

please check the return value of `awaitClose` - is it `true` or `false`? By default it does not wait as mentioned in the javadocs - that was the reason why I explicitely asked if you do check it.

---

<div class="post-metadata">

### Author: ![marchuss](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@marchuss](https://discuss.elastic.co/u/marchuss)
#### Post date: [February 4, 2020, 8:41am UTC](https://discuss.elastic.co/t/rest-high-level-client-gracefully-flushing-bulk-processor-as-jvm-exits/217368/5 "2020-02-04T08:41:25Z")

</div>

Sorry for the delay - I have now checked the return value and it is returning true (this is with an explicit timeout specified).

Thanks

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [February 4, 2020, 9:02am UTC](https://discuss.elastic.co/t/rest-high-level-client-gracefully-flushing-bulk-processor-as-jvm-exits/217368/6 "2020-02-04T09:02:49Z")

</div>

can you share the code of doing that, so it is easier to follow?

---

<div class="post-metadata">

### Author: ![marchuss](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@marchuss](https://discuss.elastic.co/u/marchuss)
#### Post date: [February 5, 2020, 8:57am UTC](https://discuss.elastic.co/t/rest-high-level-client-gracefully-flushing-bulk-processor-as-jvm-exits/217368/7 "2020-02-05T08:57:42Z")

</div>

Finally figured it out. In Spring Boot you need to override the taskScheduler Bean to waitForTasksTocCompleteOnShutdown. Something like this:

```
    @Bean
    public ThreadPoolTaskScheduler taskScheduler(){
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setPoolSize(5);
        threadPoolTaskScheduler.setThreadNamePrefix("ThreadPoolTaskScheduler");
        threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
        threadPoolTaskScheduler.setAwaitTerminationSeconds(2);
        return threadPoolTaskScheduler;
    }

```

This is because the default taskScheduler will interrupt the executing thread and not allow the bulkProcessor to flush items.

---

<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: [March 4, 2020, 8:57am UTC](https://discuss.elastic.co/t/rest-high-level-client-gracefully-flushing-bulk-processor-as-jvm-exits/217368/8 "2020-03-04T08:57:48Z")

</div>

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