mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-05-03 03:43:54 +01:00
Ensure shutdown hook is registered when a handler is added
Closes gh-34627
This commit is contained in:
+1
@@ -170,6 +170,7 @@ class SpringApplicationShutdownHook implements Runnable {
|
||||
@Override
|
||||
public void add(Runnable action) {
|
||||
Assert.notNull(action, "Action must not be null");
|
||||
addRuntimeShutdownHookIfNecessary();
|
||||
synchronized (SpringApplicationShutdownHook.class) {
|
||||
assertNotInProgress();
|
||||
this.actions.add(action);
|
||||
|
||||
+9
@@ -57,6 +57,15 @@ class SpringApplicationShutdownHookTests {
|
||||
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shutdownHookIsNotAddedUntilHandlerIsRegistered() {
|
||||
TestSpringApplicationShutdownHook shutdownHook = new TestSpringApplicationShutdownHook();
|
||||
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isFalse();
|
||||
shutdownHook.getHandlers().add(() -> {
|
||||
});
|
||||
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void runClosesContextsBeforeRunningHandlerActions() {
|
||||
TestSpringApplicationShutdownHook shutdownHook = new TestSpringApplicationShutdownHook();
|
||||
|
||||
Reference in New Issue
Block a user