From 789b8ca53b2e3478139a01ae1837a810b145968c Mon Sep 17 00:00:00 2001 From: Patrick Strawderman Date: Fri, 10 Nov 2023 11:50:39 -0800 Subject: [PATCH] Lazily initialize Executor in ScheduledDataLoaderRegistry builder Calling ScheduledDataLoaderRegistry.newScheduledRegistry would create a new ScheduledExecutorService on every call, regardless of whether a custom one was supplied. Move creation of the default ScheduledExecutorService from the builder field to the build method to avoid the issue. --- .../dataloader/registries/ScheduledDataLoaderRegistry.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/dataloader/registries/ScheduledDataLoaderRegistry.java b/src/main/java/org/dataloader/registries/ScheduledDataLoaderRegistry.java index fada66d..28b13e0 100644 --- a/src/main/java/org/dataloader/registries/ScheduledDataLoaderRegistry.java +++ b/src/main/java/org/dataloader/registries/ScheduledDataLoaderRegistry.java @@ -257,7 +257,7 @@ public static class Builder { private final Map> dataLoaders = new LinkedHashMap<>(); private final Map, DispatchPredicate> dataLoaderPredicates = new LinkedHashMap<>(); private DispatchPredicate dispatchPredicate = DispatchPredicate.DISPATCH_ALWAYS; - private ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); + private ScheduledExecutorService scheduledExecutorService; private Duration schedule = Duration.ofMillis(10); private boolean tickerMode = false; @@ -348,6 +348,9 @@ public Builder tickerMode(boolean tickerMode) { * @return the newly built {@link ScheduledDataLoaderRegistry} */ public ScheduledDataLoaderRegistry build() { + if (scheduledExecutorService == null) { + scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); + } return new ScheduledDataLoaderRegistry(this); } }