Ensure shutdown hook is registered when a handler is added
Closes gh-34627
This commit is contained in:
parent
3dac258aa8
commit
28b7c583a9
|
@ -170,6 +170,7 @@ class SpringApplicationShutdownHook implements Runnable {
|
||||||
@Override
|
@Override
|
||||||
public void add(Runnable action) {
|
public void add(Runnable action) {
|
||||||
Assert.notNull(action, "Action must not be null");
|
Assert.notNull(action, "Action must not be null");
|
||||||
|
addRuntimeShutdownHookIfNecessary();
|
||||||
synchronized (SpringApplicationShutdownHook.class) {
|
synchronized (SpringApplicationShutdownHook.class) {
|
||||||
assertNotInProgress();
|
assertNotInProgress();
|
||||||
this.actions.add(action);
|
this.actions.add(action);
|
||||||
|
|
|
@ -57,6 +57,15 @@ class SpringApplicationShutdownHookTests {
|
||||||
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isTrue();
|
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shutdownHookIsNotAddedUntilHandlerIsRegistered() {
|
||||||
|
TestSpringApplicationShutdownHook shutdownHook = new TestSpringApplicationShutdownHook();
|
||||||
|
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isFalse();
|
||||||
|
shutdownHook.getHandlers().add(() -> {
|
||||||
|
});
|
||||||
|
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void runClosesContextsBeforeRunningHandlerActions() {
|
void runClosesContextsBeforeRunningHandlerActions() {
|
||||||
TestSpringApplicationShutdownHook shutdownHook = new TestSpringApplicationShutdownHook();
|
TestSpringApplicationShutdownHook shutdownHook = new TestSpringApplicationShutdownHook();
|
||||||
|
|
Loading…
Reference in New Issue