Q: Wait for elasticsearch to finish bulkAsync save I'm writing application that writes to Elasticsearch. I read files from some directory, process and save the output. The files are large (millions of lines) and I need to do it as quickly as possible, so in a loop I create bulk request with 1,000 indexRequests and save them using using bulkAsync method. When processing is done I want to exit application. But when I exit it seems not all processing has finished and I end up with a lot of documents missing. When application finishes I get:
2019-10-15T20:00:31,343 ERROR [I/O dispatcher 3] o.m.g.e.i.RequestSender$esBulkSaveListener$1:
org.apache.http.ConnectionClosedException: Connection closed unexpectedly
        at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.closed(HttpAsyncRequestExecutor.java:146)
        at org.apache.http.impl.nio.client.InternalIODispatch.onClosed(InternalIODispatch.java:71)
        at org.apache.http.impl.nio.client.InternalIODispatch.onClosed(InternalIODispatch.java:39)
        at org.apache.http.impl.nio.reactor.AbstractIODispatch.disconnected(AbstractIODispatch.java:100)
        at org.apache.http.impl.nio.reactor.BaseIOReactor.sessionClosed(BaseIOReactor.java:277)
        at org.apache.http.impl.nio.reactor.AbstractIOReactor.processClosedSessions(AbstractIOReactor.java:449)
        at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:283)
        at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
        at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591)
        at java.base/java.lang.Thread.run(Thread.java:834)

I don't see this if I don't exit application right after processing, but then it looks I have no way of exiting it at all.
Is there any way to wait for elasticsearch to finish before exiting?

A: Is there a reason why you need to write a custom application for that?
Logstash or Filebeat do read files and integrate with Elasticsearch really well, Logstash handles failures, backpressure.
Looking at your error, it could be that you're sending too much data to cluster, it cannot handle that load and it crashes. I'd also recommend checking elasticsearch logs, they might be useful and give you a hint of what's not right.

A: 
"But when I exit it seems not all processing has finished and I end up
  with a lot of documents missing"

you need to make sure that all the bulkRequest response without any error.  The bulkSize does matters depend on what type of analysis it required. Try tuning for the suitable bulkSize.

".. and I need to do it as quickly as possible.."

the multiple thread need to be done for handling concurrent bulkRequest at a time. it's depend on the spec of your es cluster how many concurrent.
plz monitor the load while bulking
