From f2f4a4a40eb01086b1c8707454522190b20d823d Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 6 Oct 2022 13:11:29 +0100 Subject: [PATCH] Space out smart lifecycle phases used for graceful shutdown Previously, the web server was stopped in the last smart lifecycle phase with graceful shutdown having begun in the previous phase. This lack of space between the two phases and after the stop phase made it hard to for other smart lifecycles to be part of the graceful shutdown process. This commit moves stop to 1024 phases before the final phase and graceful shutdown a further 1024 phases before that, allowing other smart lifecycles to run between graceful shutdown and stop and also after stop. Closes gh-31714 --- .../boot/web/context/WebServerGracefulShutdownLifecycle.java | 2 +- .../boot/web/reactive/context/WebServerStartStopLifecycle.java | 3 ++- .../boot/web/servlet/context/WebServerStartStopLifecycle.java | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerGracefulShutdownLifecycle.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerGracefulShutdownLifecycle.java index 7bd1dbf4ef..c96ea89930 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerGracefulShutdownLifecycle.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerGracefulShutdownLifecycle.java @@ -31,7 +31,7 @@ public final class WebServerGracefulShutdownLifecycle implements SmartLifecycle * {@link SmartLifecycle#getPhase() SmartLifecycle phase} in which graceful shutdown * of the web server is performed. */ - public static final int SMART_LIFECYCLE_PHASE = SmartLifecycle.DEFAULT_PHASE; + public static final int SMART_LIFECYCLE_PHASE = SmartLifecycle.DEFAULT_PHASE - 1024; private final WebServer webServer; diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/WebServerStartStopLifecycle.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/WebServerStartStopLifecycle.java index ebd2b7b72f..d50172824a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/WebServerStartStopLifecycle.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/WebServerStartStopLifecycle.java @@ -16,6 +16,7 @@ package org.springframework.boot.web.reactive.context; +import org.springframework.boot.web.context.WebServerGracefulShutdownLifecycle; import org.springframework.boot.web.server.WebServer; import org.springframework.context.SmartLifecycle; @@ -54,7 +55,7 @@ class WebServerStartStopLifecycle implements SmartLifecycle { @Override public int getPhase() { - return Integer.MAX_VALUE - 1; + return WebServerGracefulShutdownLifecycle.SMART_LIFECYCLE_PHASE - 1024; } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/WebServerStartStopLifecycle.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/WebServerStartStopLifecycle.java index c5c07627be..a8e44ef50e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/WebServerStartStopLifecycle.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/WebServerStartStopLifecycle.java @@ -16,6 +16,7 @@ package org.springframework.boot.web.servlet.context; +import org.springframework.boot.web.context.WebServerGracefulShutdownLifecycle; import org.springframework.boot.web.server.WebServer; import org.springframework.context.SmartLifecycle; @@ -59,7 +60,7 @@ class WebServerStartStopLifecycle implements SmartLifecycle { @Override public int getPhase() { - return Integer.MAX_VALUE - 1; + return WebServerGracefulShutdownLifecycle.SMART_LIFECYCLE_PHASE - 1024; } }