Skip to content

Commit 6c87d75

Browse files
committed
Shutsdown executor if its was auto added by us
1 parent b90e4cd commit 6c87d75

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/main/java/org/dataloader/registries/ScheduledDataLoaderRegistry.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public class ScheduledDataLoaderRegistry extends DataLoaderRegistry implements A
5858
private final Map<DataLoader<?, ?>, DispatchPredicate> dataLoaderPredicates = new ConcurrentHashMap<>();
5959
private final DispatchPredicate dispatchPredicate;
6060
private final ScheduledExecutorService scheduledExecutorService;
61+
private final boolean defaultExecutorUsed;
62+
6163
private final Duration schedule;
6264
private final boolean tickerMode;
6365
private volatile boolean closed;
@@ -66,6 +68,7 @@ private ScheduledDataLoaderRegistry(Builder builder) {
6668
super();
6769
this.dataLoaders.putAll(builder.dataLoaders);
6870
this.scheduledExecutorService = builder.scheduledExecutorService;
71+
this.defaultExecutorUsed = builder.defaultExecutorUsed;
6972
this.schedule = builder.schedule;
7073
this.tickerMode = builder.tickerMode;
7174
this.closed = false;
@@ -79,6 +82,16 @@ private ScheduledDataLoaderRegistry(Builder builder) {
7982
@Override
8083
public void close() {
8184
closed = true;
85+
if (defaultExecutorUsed) {
86+
scheduledExecutorService.shutdown();
87+
}
88+
}
89+
90+
/**
91+
* @return executor being used by this registry
92+
*/
93+
public ScheduledExecutorService getScheduledExecutorService() {
94+
return scheduledExecutorService;
8295
}
8396

8497
/**
@@ -258,9 +271,18 @@ public static class Builder {
258271
private final Map<DataLoader<?, ?>, DispatchPredicate> dataLoaderPredicates = new LinkedHashMap<>();
259272
private DispatchPredicate dispatchPredicate = DispatchPredicate.DISPATCH_ALWAYS;
260273
private ScheduledExecutorService scheduledExecutorService;
274+
private boolean defaultExecutorUsed = false;
261275
private Duration schedule = Duration.ofMillis(10);
262276
private boolean tickerMode = false;
263277

278+
/**
279+
* If you provide a {@link ScheduledExecutorService} then it will NOT be shutdown when
280+
* {@link ScheduledDataLoaderRegistry#close()} is called. This is left to the code that made this setup code
281+
*
282+
* @param executorService the executor service to run the ticker on
283+
*
284+
* @return this builder for a fluent pattern
285+
*/
264286
public Builder scheduledExecutorService(ScheduledExecutorService executorService) {
265287
this.scheduledExecutorService = nonNull(executorService);
266288
return this;
@@ -350,6 +372,7 @@ public Builder tickerMode(boolean tickerMode) {
350372
public ScheduledDataLoaderRegistry build() {
351373
if (scheduledExecutorService == null) {
352374
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
375+
defaultExecutorUsed = true;
353376
}
354377
return new ScheduledDataLoaderRegistry(this);
355378
}

src/test/java/org/dataloader/registries/ScheduledDataLoaderRegistryTest.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.List;
1313
import java.util.concurrent.CompletableFuture;
1414
import java.util.concurrent.Executors;
15+
import java.util.concurrent.ScheduledExecutorService;
1516
import java.util.concurrent.atomic.AtomicBoolean;
1617
import java.util.concurrent.atomic.AtomicInteger;
1718

@@ -285,7 +286,7 @@ public void test_can_tick_after_first_dispatch_for_chain_data_loaders() {
285286
assertThat(registry.isTickerMode(), equalTo(true));
286287

287288
int count = registry.dispatchAllWithCount();
288-
assertThat(count,equalTo(1));
289+
assertThat(count, equalTo(1));
289290

290291
await().atMost(TWO_SECONDS).untilAtomic(done, is(true));
291292

@@ -314,7 +315,7 @@ public void test_chain_data_loaders_will_hang_if_not_in_ticker_mode() {
314315
assertThat(registry.isTickerMode(), equalTo(false));
315316

316317
int count = registry.dispatchAllWithCount();
317-
assertThat(count,equalTo(1));
318+
assertThat(count, equalTo(1));
318319

319320
try {
320321
await().atMost(TWO_SECONDS).untilAtomic(done, is(true));
@@ -323,4 +324,25 @@ public void test_chain_data_loaders_will_hang_if_not_in_ticker_mode() {
323324
}
324325
registry.close();
325326
}
327+
328+
public void test_executors_are_shutdown() {
329+
ScheduledDataLoaderRegistry registry = ScheduledDataLoaderRegistry.newScheduledRegistry().build();
330+
331+
ScheduledExecutorService executorService = registry.getScheduledExecutorService();
332+
assertThat(executorService.isShutdown(), equalTo(false));
333+
registry.close();
334+
assertThat(executorService.isShutdown(), equalTo(true));
335+
336+
executorService = Executors.newSingleThreadScheduledExecutor();
337+
registry = ScheduledDataLoaderRegistry.newScheduledRegistry()
338+
.scheduledExecutorService(executorService).build();
339+
340+
executorService = registry.getScheduledExecutorService();
341+
assertThat(executorService.isShutdown(), equalTo(false));
342+
registry.close();
343+
// if they provide the executor, we don't close it down
344+
assertThat(executorService.isShutdown(), equalTo(false));
345+
346+
347+
}
326348
}

0 commit comments

Comments
 (0)