Skip to content

Commit

Permalink
bugfix: shut down executor threads due to PREPARE_SHUTDOWN event, ins…
Browse files Browse the repository at this point in the history
…tead of SERVER_SHUTDOWN,

thus avoiding exceptions when events run during server shutdown
  • Loading branch information
lprimak committed Aug 18, 2024
1 parent 133d648 commit 6d2cdde
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2017-2022] Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2017-2024] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -137,8 +137,10 @@ public void event(Event event) {
if (null == threadPoolExecutor) {
initialiseThreadPools();
}
} else if (event.is(EventTypes.SERVER_SHUTDOWN)) {
} else if (event.is(EventTypes.PREPARE_SHUTDOWN)) {
terminateThreadPools();
} else if (event.is(EventTypes.SERVER_SHUTDOWN)) {
awaitTerminationOfThreadPools();
}
}

Expand All @@ -149,7 +151,13 @@ private void terminateThreadPools() {
}
threadPoolExecutor.shutdown();
scheduledThreadPoolExecutor.shutdown();
}

private void awaitTerminationOfThreadPools() {
if (threadPoolExecutor == null) {
// we didn't initialize yet
return;
}
// Wait until the schedulers actually terminate
try {
threadPoolExecutor.awaitTermination(5, TimeUnit.SECONDS);
Expand Down Expand Up @@ -190,6 +198,7 @@ private void initialiseThreadPools() {
return t;
});
scheduledThreadPoolExecutor.setRemoveOnCancelPolicy(true);
scheduledThreadPoolExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);


} else {
Expand Down Expand Up @@ -225,6 +234,7 @@ public void uncaughtException(Thread thread, Throwable thrwbl) {
return t;
});
scheduledThreadPoolExecutor.setRemoveOnCancelPolicy(true);
scheduledThreadPoolExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
}

}
Expand Down

0 comments on commit 6d2cdde

Please sign in to comment.