fix: shutdown method blocks until task executor shutdown completes (#873)

* 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>
This commit is contained in:
jarebudev 2024-04-06 12:47:42 +01:00 committed by GitHub
parent dd671adfa3
commit 8dec14fbea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 4 deletions

View File

@ -12,6 +12,7 @@ import java.util.UUID;
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;
/**
@ -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 -> {
@ -146,13 +148,19 @@ class EventSupport {
}
/**
* 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();
}
}