diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/WebServerGracefulShutdownLifecycle.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerGracefulShutdownLifecycle.java similarity index 62% rename from spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/WebServerGracefulShutdownLifecycle.java rename to spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerGracefulShutdownLifecycle.java index 8e306c1ad3..7bd1dbf4ef 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/WebServerGracefulShutdownLifecycle.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerGracefulShutdownLifecycle.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,24 +14,35 @@ * limitations under the License. */ -package org.springframework.boot.web.servlet.context; +package org.springframework.boot.web.context; import org.springframework.boot.web.server.WebServer; import org.springframework.context.SmartLifecycle; /** - * {@link SmartLifecycle} to trigger {@link WebServer} graceful shutdown in a - * {@link ServletWebServerApplicationContext}. + * {@link SmartLifecycle} to trigger {@link WebServer} graceful shutdown. * * @author Andy Wilkinson + * @since 2.5.0 */ -class WebServerGracefulShutdownLifecycle implements SmartLifecycle { +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; private final WebServer webServer; private volatile boolean running; - WebServerGracefulShutdownLifecycle(WebServer webServer) { + /** + * Creates a new {@code WebServerGracefulShutdownLifecycle} that will gracefully shut + * down the given {@code webServer}. + * @param webServer web server to shut down gracefully + */ + public WebServerGracefulShutdownLifecycle(WebServer webServer) { this.webServer = webServer; } @@ -56,4 +67,9 @@ class WebServerGracefulShutdownLifecycle implements SmartLifecycle { return this.running; } + @Override + public int getPhase() { + return SMART_LIFECYCLE_PHASE; + } + } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/ReactiveWebServerApplicationContext.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/ReactiveWebServerApplicationContext.java index 942c42bbdb..58528b6eac 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/ReactiveWebServerApplicationContext.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/ReactiveWebServerApplicationContext.java @@ -21,6 +21,7 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.boot.availability.AvailabilityChangeEvent; import org.springframework.boot.availability.ReadinessState; import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext; +import org.springframework.boot.web.context.WebServerGracefulShutdownLifecycle; import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory; import org.springframework.boot.web.server.WebServer; import org.springframework.context.ApplicationContextException; @@ -92,7 +93,7 @@ public class ReactiveWebServerApplicationContext extends GenericReactiveWebAppli boolean lazyInit = getBeanFactory().getBeanDefinition(webServerFactoryBeanName).isLazyInit(); this.serverManager = new WebServerManager(this, webServerFactory, this::getHttpHandler, lazyInit); getBeanFactory().registerSingleton("webServerGracefulShutdown", - new WebServerGracefulShutdownLifecycle(this.serverManager)); + new WebServerGracefulShutdownLifecycle(this.serverManager.getWebServer())); getBeanFactory().registerSingleton("webServerStartStop", new WebServerStartStopLifecycle(this.serverManager)); createWebServer.end(); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/WebServerGracefulShutdownLifecycle.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/WebServerGracefulShutdownLifecycle.java deleted file mode 100644 index 30fc0690a4..0000000000 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/WebServerGracefulShutdownLifecycle.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2012-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.web.reactive.context; - -import org.springframework.boot.web.server.WebServer; -import org.springframework.context.SmartLifecycle; - -/** - * {@link SmartLifecycle} to trigger {@link WebServer} graceful shutdown in a - * {@link ReactiveWebServerApplicationContext}. - * - * @author Andy Wilkinson - */ -class WebServerGracefulShutdownLifecycle implements SmartLifecycle { - - private final WebServerManager serverManager; - - private volatile boolean running; - - WebServerGracefulShutdownLifecycle(WebServerManager serverManager) { - this.serverManager = serverManager; - } - - @Override - public void start() { - this.running = true; - } - - @Override - public void stop() { - throw new UnsupportedOperationException("Stop must not be invoked directly"); - } - - @Override - public void stop(Runnable callback) { - this.running = false; - this.serverManager.shutDownGracefully(callback); - } - - @Override - public boolean isRunning() { - return this.running; - } - -} diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/WebServerManager.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/WebServerManager.java index 03bcf7412c..bf6d3c08be 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/WebServerManager.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/WebServerManager.java @@ -21,6 +21,7 @@ import java.util.function.Supplier; import reactor.core.publisher.Mono; import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory; +import org.springframework.boot.web.server.GracefulShutdownCallback; import org.springframework.boot.web.server.WebServer; import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.ServerHttpRequest; @@ -56,8 +57,8 @@ class WebServerManager { .publishEvent(new ReactiveWebServerInitializedEvent(this.webServer, this.applicationContext)); } - void shutDownGracefully(Runnable callback) { - this.webServer.shutDownGracefully((result) -> callback.run()); + void shutDownGracefully(GracefulShutdownCallback callback) { + this.webServer.shutDownGracefully(callback); } void stop() { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/ServletWebServerApplicationContext.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/ServletWebServerApplicationContext.java index 5292ff7fd8..d1f158d819 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/ServletWebServerApplicationContext.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/context/ServletWebServerApplicationContext.java @@ -40,6 +40,7 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.boot.availability.AvailabilityChangeEvent; import org.springframework.boot.availability.ReadinessState; import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext; +import org.springframework.boot.web.context.WebServerGracefulShutdownLifecycle; import org.springframework.boot.web.server.WebServer; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletContextInitializer;