Skip to content

Commit 1cd8029

Browse files
authored
Merge pull request #144 from graphql-java/close-executor
Shuts down executor if its was auto added by our code
2 parents b90e4cd + 69efc4e commit 1cd8029

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

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

+22
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ 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;
6162
private final Duration schedule;
6263
private final boolean tickerMode;
6364
private volatile boolean closed;
@@ -66,6 +67,7 @@ private ScheduledDataLoaderRegistry(Builder builder) {
6667
super();
6768
this.dataLoaders.putAll(builder.dataLoaders);
6869
this.scheduledExecutorService = builder.scheduledExecutorService;
70+
this.defaultExecutorUsed = builder.defaultExecutorUsed;
6971
this.schedule = builder.schedule;
7072
this.tickerMode = builder.tickerMode;
7173
this.closed = false;
@@ -79,6 +81,16 @@ private ScheduledDataLoaderRegistry(Builder builder) {
7981
@Override
8082
public void close() {
8183
closed = true;
84+
if (defaultExecutorUsed) {
85+
scheduledExecutorService.shutdown();
86+
}
87+
}
88+
89+
/**
90+
* @return executor being used by this registry
91+
*/
92+
public ScheduledExecutorService getScheduledExecutorService() {
93+
return scheduledExecutorService;
8294
}
8395

8496
/**
@@ -258,9 +270,18 @@ public static class Builder {
258270
private final Map<DataLoader<?, ?>, DispatchPredicate> dataLoaderPredicates = new LinkedHashMap<>();
259271
private DispatchPredicate dispatchPredicate = DispatchPredicate.DISPATCH_ALWAYS;
260272
private ScheduledExecutorService scheduledExecutorService;
273+
private boolean defaultExecutorUsed = false;
261274
private Duration schedule = Duration.ofMillis(10);
262275
private boolean tickerMode = false;
263276

277+
/**
278+
* If you provide a {@link ScheduledExecutorService} then it will NOT be shutdown when
279+
* {@link ScheduledDataLoaderRegistry#close()} is called. This is left to the code that made this setup code
280+
*
281+
* @param executorService the executor service to run the ticker on
282+
*
283+
* @return this builder for a fluent pattern
284+
*/
264285
public Builder scheduledExecutorService(ScheduledExecutorService executorService) {
265286
this.scheduledExecutorService = nonNull(executorService);
266287
return this;
@@ -350,6 +371,7 @@ public Builder tickerMode(boolean tickerMode) {
350371
public ScheduledDataLoaderRegistry build() {
351372
if (scheduledExecutorService == null) {
352373
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
374+
defaultExecutorUsed = true;
353375
}
354376
return new ScheduledDataLoaderRegistry(this);
355377
}

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

+24-2
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)