From 361f500ed5d31ffb0abe5252e7bf7c3daf323dbb Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 30 Apr 2013 10:17:39 +0100 Subject: [PATCH] [bs-28] Add /shutdown endpoint to service apps Disabled by default use container.allow_shutdown=true to switch it on. Then POST to /shutdown to shut down the app. [#48059059] --- .../spring-bootstrap-service-sample/pom.xml | 4 +- .../src/main/resources/application.properties | 1 + .../service/ManagementAutoConfiguration.java | 6 +- .../service/ShutdownAutoConfiguration.java | 44 +++++++++ .../service/VarzAutoConfiguration.java | 2 +- .../properties/ContainerProperties.java | 10 ++ .../service/shutdown/ShutdownEndpoint.java | 94 +++++++++++++++++++ .../jetty/JettyEmbeddedServletContainer.java | 4 + 8 files changed, 160 insertions(+), 5 deletions(-) create mode 100644 spring-bootstrap-service/src/main/java/org/springframework/bootstrap/autoconfigure/service/ShutdownAutoConfiguration.java create mode 100644 spring-bootstrap-service/src/main/java/org/springframework/bootstrap/service/shutdown/ShutdownEndpoint.java diff --git a/spring-bootstrap-samples/spring-bootstrap-service-sample/pom.xml b/spring-bootstrap-samples/spring-bootstrap-service-sample/pom.xml index f728796133..ab1f1bf3fd 100644 --- a/spring-bootstrap-samples/spring-bootstrap-service-sample/pom.xml +++ b/spring-bootstrap-samples/spring-bootstrap-service-sample/pom.xml @@ -17,7 +17,7 @@ tomcat - false + true @@ -33,7 +33,7 @@ jetty - true + false diff --git a/spring-bootstrap-samples/spring-bootstrap-service-sample/src/main/resources/application.properties b/spring-bootstrap-samples/spring-bootstrap-service-sample/src/main/resources/application.properties index 308f12caed..437acb0b81 100644 --- a/spring-bootstrap-samples/spring-bootstrap-service-sample/src/main/resources/application.properties +++ b/spring-bootstrap-samples/spring-bootstrap-service-sample/src/main/resources/application.properties @@ -1,6 +1,7 @@ logging.file: /tmp/logs/app.log container.port: 8080 container.management_port: 8080 +container.allow_shutdown: true container.tomcat.basedir: target/tomcat container.tomcat.access_log_pattern: %h %t "%r" %s %b security.require_ssl: false diff --git a/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/autoconfigure/service/ManagementAutoConfiguration.java b/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/autoconfigure/service/ManagementAutoConfiguration.java index 544d90f7e7..5988cc36e7 100644 --- a/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/autoconfigure/service/ManagementAutoConfiguration.java +++ b/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/autoconfigure/service/ManagementAutoConfiguration.java @@ -42,7 +42,8 @@ public class ManagementAutoConfiguration implements ApplicationContextAware, @ConditionalOnExpression("${container.port:8080} == ${container.management_port:8080}") @Configuration - @Import({ VarzAutoConfiguration.class, HealthzAutoConfiguration.class }) + @Import({ VarzAutoConfiguration.class, HealthzAutoConfiguration.class, + ShutdownAutoConfiguration.class }) public static class ManagementEndpointsConfiguration { } @@ -71,7 +72,8 @@ public class ManagementAutoConfiguration implements ApplicationContextAware, AnnotationConfigEmbeddedWebApplicationContext context = new AnnotationConfigEmbeddedWebApplicationContext(); context.setParent(this.parent); context.register(ManagementContainerConfiguration.class, - VarzAutoConfiguration.class, HealthzAutoConfiguration.class); + VarzAutoConfiguration.class, HealthzAutoConfiguration.class, + ShutdownAutoConfiguration.class); context.refresh(); this.context = context; } diff --git a/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/autoconfigure/service/ShutdownAutoConfiguration.java b/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/autoconfigure/service/ShutdownAutoConfiguration.java new file mode 100644 index 0000000000..640218e656 --- /dev/null +++ b/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/autoconfigure/service/ShutdownAutoConfiguration.java @@ -0,0 +1,44 @@ +/* + * Copyright 2012-2013 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 + * + * http://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.bootstrap.autoconfigure.service; + +import javax.servlet.Servlet; + +import org.springframework.bootstrap.context.annotation.ConditionalOnClass; +import org.springframework.bootstrap.context.annotation.ConditionalOnMissingBean; +import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration; +import org.springframework.bootstrap.service.shutdown.ShutdownEndpoint; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.DispatcherServlet; + +/** + * {@link EnableAutoConfiguration Auto-configuration} for /shutdown endpoint. + * + * @author Dave Syer + */ +@Configuration +@ConditionalOnClass({ Servlet.class, DispatcherServlet.class }) +@ConditionalOnMissingBean({ ShutdownEndpoint.class }) +public class ShutdownAutoConfiguration { + + @Bean + public ShutdownEndpoint shutdownEndpoint() { + return new ShutdownEndpoint(); + } + +} diff --git a/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/autoconfigure/service/VarzAutoConfiguration.java b/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/autoconfigure/service/VarzAutoConfiguration.java index 156f7933eb..ac82365f57 100644 --- a/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/autoconfigure/service/VarzAutoConfiguration.java +++ b/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/autoconfigure/service/VarzAutoConfiguration.java @@ -31,7 +31,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.DispatcherServlet; /** - * {@link EnableAutoConfiguration Auto-configuration} for /healthz endpoint. + * {@link EnableAutoConfiguration Auto-configuration} for /varz endpoint. * * @author Dave Syer */ diff --git a/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/service/properties/ContainerProperties.java b/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/service/properties/ContainerProperties.java index 01c8b8f318..8fb50cb1c9 100644 --- a/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/service/properties/ContainerProperties.java +++ b/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/service/properties/ContainerProperties.java @@ -34,6 +34,8 @@ public class ContainerProperties { private String contextPath; + private boolean allowShutdown = false; + private Tomcat tomcat = new Tomcat(); public Tomcat getTomcat() { @@ -64,6 +66,14 @@ public class ContainerProperties { this.managementPort = managementPort; } + public boolean isAllowShutdown() { + return this.allowShutdown; + } + + public void setAllowShutdown(boolean allowShutdown) { + this.allowShutdown = allowShutdown; + } + public static class Tomcat { private String accessLogPattern; diff --git a/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/service/shutdown/ShutdownEndpoint.java b/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/service/shutdown/ShutdownEndpoint.java new file mode 100644 index 0000000000..2ae1acc4b0 --- /dev/null +++ b/spring-bootstrap-service/src/main/java/org/springframework/bootstrap/service/shutdown/ShutdownEndpoint.java @@ -0,0 +1,94 @@ +/* + * Copyright 2012-2013 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 + * + * http://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.bootstrap.service.shutdown; + +import java.util.Collections; +import java.util.Map; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.bootstrap.service.properties.ContainerProperties; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.ApplicationListener; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.context.support.ServletRequestHandledEvent; + +/** + * @author Dave Syer + * + */ +@Controller +public class ShutdownEndpoint implements ApplicationContextAware, + ApplicationListener { + + private static Log logger = LogFactory.getLog(ShutdownEndpoint.class); + + private ConfigurableApplicationContext context; + + @Autowired + private ContainerProperties configuration = new ContainerProperties(); + + @RequestMapping(value = "${endpoints.shutdown.path:/shutdown}", method = RequestMethod.POST) + @ResponseBody + public Map shutdown() { + if (this.configuration.isAllowShutdown()) { + return Collections. singletonMap("message", + "Shutting down, bye..."); + } else { + return Collections. singletonMap("message", + "Shutdown not enabled, sorry."); + } + } + + @Override + public void setApplicationContext(ApplicationContext context) throws BeansException { + if (context instanceof ConfigurableApplicationContext) { + this.context = (ConfigurableApplicationContext) context; + } + } + + @Override + public void onApplicationEvent(ServletRequestHandledEvent event) { + + if (this.context != null && this.configuration.isAllowShutdown()) { + + new Thread(new Runnable() { + @Override + public void run() { + logger.info("Shutting down Spring in response to admin request"); + ConfigurableApplicationContext context = ShutdownEndpoint.this.context; + ApplicationContext parent = context.getParent(); + context.close(); + if (parent != null + && parent instanceof ConfigurableApplicationContext) { + context = (ConfigurableApplicationContext) parent; + context.close(); + parent = context.getParent(); + } + } + }).start(); + + } + } + +} diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/jetty/JettyEmbeddedServletContainer.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/jetty/JettyEmbeddedServletContainer.java index a46f2cac03..7c533994a9 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/jetty/JettyEmbeddedServletContainer.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/jetty/JettyEmbeddedServletContainer.java @@ -56,7 +56,11 @@ public class JettyEmbeddedServletContainer implements EmbeddedServletContainer { @Override public synchronized void stop() { try { + this.server.setGracefulShutdown(10000); this.server.stop(); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + // No drama } catch (Exception ex) { throw new EmbeddedServletContainerException( "Unable to stop embedded Jetty servlet container", ex);