diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 8647ddae3af..0142e34a691 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -109,7 +109,7 @@ public class ServerProperties { /** * Type of shutdown that the server will support. */ - private Shutdown shutdown = Shutdown.IMMEDIATE; + private Shutdown shutdown = Shutdown.GRACEFUL; @NestedConfigurationProperty private Ssl ssl; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryCustomizerTests.java index 66e0ae47710..73e1348c356 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryCustomizerTests.java @@ -186,11 +186,11 @@ class ServletWebServerFactoryCustomizerTests { @Test void whenShutdownPropertyIsSetThenShutdownIsCustomized() { Map map = new HashMap<>(); - map.put("server.shutdown", "graceful"); + map.put("server.shutdown", "immediate"); bindProperties(map); ConfigurableServletWebServerFactory factory = mock(ConfigurableServletWebServerFactory.class); this.customizer.customize(factory); - then(factory).should().setShutdown(assertArg((shutdown) -> assertThat(shutdown).isEqualTo(Shutdown.GRACEFUL))); + then(factory).should().setShutdown(assertArg((shutdown) -> assertThat(shutdown).isEqualTo(Shutdown.IMMEDIATE))); } private void bindProperties(Map map) { diff --git a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/web/graceful-shutdown.adoc b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/web/graceful-shutdown.adoc index 8490b8babae..02a02304e9e 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/web/graceful-shutdown.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/web/graceful-shutdown.adoc @@ -1,29 +1,10 @@ [[web.graceful-shutdown]] = Graceful Shutdown -Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and servlet-based web applications. +Graceful shutdown is enabled by default with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and servlet-based web applications. It occurs as part of closing the application context and is performed in the earliest phase of stopping `SmartLifecycle` beans. This stop processing uses a timeout which provides a grace period during which existing requests will be allowed to complete but no new requests will be permitted. -The exact way in which new requests are not permitted varies depending on the web server that is being used. -Implementations may stop accepting requests at the network layer, or they may return a response with a specific HTTP status code or HTTP header. -The use of persistent connections can also change the way that requests stop being accepted. - -TIP: To learn about more the specific method used with your web server, see the `shutDownGracefully` API documentation for javadoc:org.springframework.boot.web.embedded.tomcat.TomcatWebServer#shutDownGracefully(org.springframework.boot.web.server.GracefulShutdownCallback)[], javadoc:org.springframework.boot.web.embedded.netty.NettyWebServer#shutDownGracefully(org.springframework.boot.web.server.GracefulShutdownCallback)[], javadoc:org.springframework.boot.web.embedded.jetty.JettyWebServer#shutDownGracefully(org.springframework.boot.web.server.GracefulShutdownCallback)[] or javadoc:org.springframework.boot.web.embedded.undertow.UndertowWebServer#shutDownGracefully(org.springframework.boot.web.server.GracefulShutdownCallback)[]. - -Jetty, Reactor Netty, and Tomcat will stop accepting new requests at the network layer. -Undertow will accept new connections but respond immediately with a service unavailable (503) response. - -NOTE: Graceful shutdown with Tomcat requires Tomcat 9.0.33 or later. - -To enable graceful shutdown, configure the configprop:server.shutdown[] property, as shown in the following example: - -[configprops,yaml] ----- -server: - shutdown: "graceful" ----- - To configure the timeout period, configure the configprop:spring.lifecycle.timeout-per-shutdown-phase[] property, as shown in the following example: [configprops,yaml] @@ -33,5 +14,34 @@ spring: timeout-per-shutdown-phase: "20s" ---- -IMPORTANT: Using graceful shutdown with your IDE may not work properly if it does not send a proper `SIGTERM` signal. +NOTE: Graceful shutdown with Tomcat requires Tomcat 9.0.33 or later. + +IMPORTANT: Shutdown in your IDE may be immediate rather than graceful if it does not send a proper `SIGTERM` signal. See the documentation of your IDE for more details. + + + +[[web.graceful-shutdown.rejecting-requests-during-the-grace-period]] +== Rejecting Requests During the Grace Period + +The exact way in which new requests are not permitted varies depending on the web server that is being used. +Implementations may stop accepting requests at the network layer, or they may return a response with a specific HTTP status code or HTTP header. +The use of persistent connections can also change the way that requests stop being accepted. + +TIP: To learn more about the specific method used with your web server, see the `shutDownGracefully` API documentation for javadoc:org.springframework.boot.web.embedded.tomcat.TomcatWebServer#shutDownGracefully(org.springframework.boot.web.server.GracefulShutdownCallback)[], javadoc:org.springframework.boot.web.embedded.netty.NettyWebServer#shutDownGracefully(org.springframework.boot.web.server.GracefulShutdownCallback)[], javadoc:org.springframework.boot.web.embedded.jetty.JettyWebServer#shutDownGracefully(org.springframework.boot.web.server.GracefulShutdownCallback)[] or javadoc:org.springframework.boot.web.embedded.undertow.UndertowWebServer#shutDownGracefully(org.springframework.boot.web.server.GracefulShutdownCallback)[]. + +Jetty, Reactor Netty, and Tomcat will stop accepting new requests at the network layer. +Undertow will accept new connections but respond immediately with a service unavailable (503) response. + + + +[[web.graceful-shutdown.disabling-graceful-shutdown]] +== Disabling Graceful Shutdown + +To disable graceful shutdown, configure the configprop:server.shutdown[] property, as shown in the following example: + +[configprops,yaml] +---- +server: + shutdown: "immediate" +----