Skip to content

Commit 3618e9d

Browse files
committed
closing logic
1 parent 1ea3164 commit 3618e9d

File tree

1 file changed

+6
-10
lines changed
  • java-client/src/main/java/co/elastic/clients/elasticsearch/_helpers/bulk

1 file changed

+6
-10
lines changed

java-client/src/main/java/co/elastic/clients/elasticsearch/_helpers/bulk/BulkIngester.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class BulkIngester<Context> implements AutoCloseable {
6262

6363
private @Nullable ScheduledFuture<?> flushTask;
6464
private @Nullable ScheduledExecutorService scheduler;
65+
private boolean isExternalScheduler = false;
6566

6667
// Current state
6768
private List<BulkOperation> operations = new ArrayList<>();
@@ -101,25 +102,20 @@ private BulkIngester(Builder<Context> builder) {
101102
this.listener = builder.listener;
102103
this.flushIntervalMillis = builder.flushIntervalMillis;
103104

104-
// Create a scheduler if needed
105-
ScheduledExecutorService scheduler = null;
106105
if (flushIntervalMillis != null || listener != null) {
107-
106+
// Create a scheduler if needed
108107
if (builder.scheduler == null) {
109-
scheduler = Executors.newScheduledThreadPool(maxRequests + 1, (r) -> {
108+
this.scheduler = Executors.newScheduledThreadPool(maxRequests + 1, (r) -> {
110109
Thread t = Executors.defaultThreadFactory().newThread(r);
111110
t.setName("bulk-ingester-executor#" + ingesterId + "#" + t.getId());
112111
t.setDaemon(true);
113112
return t;
114113
});
115-
116-
// Keep it, we'll have to close it.
117-
this.scheduler = scheduler;
118114
} else {
119115
// It's not ours, we will not close it.
120-
scheduler = builder.scheduler;
116+
this.scheduler = builder.scheduler;
117+
this.isExternalScheduler = true;
121118
}
122-
123119
}
124120

125121
if (flushIntervalMillis != null) {
@@ -398,7 +394,7 @@ public void close() {
398394
flushTask.cancel(false);
399395
}
400396

401-
if (scheduler != null) {
397+
if (scheduler != null && !isExternalScheduler) {
402398
scheduler.shutdownNow();
403399
}
404400
}

0 commit comments

Comments
 (0)