Skip to content

Commit

Permalink
fix: shutdown method blocks until task executor shutdown completes (#873
Browse files Browse the repository at this point in the history
)

* shutdown method blocks until task executor shutdown completes

Signed-off-by: jarebudev <23311805+jarebudev@users.noreply.github.com>

* addressed sonar issue with not handling interrupt during waiting

Signed-off-by: jarebudev <23311805+jarebudev@users.noreply.github.com>

---------

Signed-off-by: jarebudev <23311805+jarebudev@users.noreply.github.com>
Co-authored-by: Michael Beemer <beeme1mr@users.noreply.github.com>
Co-authored-by: Todd Baert <todd.baert@dynatrace.com>
  • Loading branch information
3 people authored Apr 6, 2024
1 parent dd671ad commit 8dec14f
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/main/java/dev/openfeature/sdk/EventSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

/**
Expand All @@ -23,6 +24,7 @@ class EventSupport {
// we use a v4 uuid as a "placeholder" for anonymous clients, since
// ConcurrentHashMap doesn't support nulls
private static final String defaultClientUuid = UUID.randomUUID().toString();
private static final int SHUTDOWN_TIMEOUT_SECONDS = 3;
private final Map<String, HandlerStore> handlerStores = new ConcurrentHashMap<>();
private final HandlerStore globalHandlerStore = new HandlerStore();
private final ExecutorService taskExecutor = Executors.newCachedThreadPool(runnable -> {
Expand Down Expand Up @@ -146,13 +148,19 @@ public void runHandler(Consumer<EventDetails> handler, EventDetails eventDetails
}

/**
* Stop the event handler task executor.
* Stop the event handler task executor and block until either termination has completed
* or timeout period has elapsed.
*/
public void shutdown() {
taskExecutor.shutdown();
try {
taskExecutor.shutdown();
} catch (Exception e) {
log.warn("Exception while attempting to shutdown task executor", e);
if (!taskExecutor.awaitTermination(SHUTDOWN_TIMEOUT_SECONDS, TimeUnit.SECONDS)) {
log.warn("Task executor did not terminate before the timeout period had elapsed");
taskExecutor.shutdownNow();
}
} catch (InterruptedException e) {
taskExecutor.shutdownNow();
Thread.currentThread().interrupt();
}
}

Expand Down

0 comments on commit 8dec14f

Please sign in to comment.