diff options
author | Steve Smokowski <ss835w@att.com> | 2020-02-13 15:50:00 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-02-13 15:50:00 +0000 |
commit | d6e85d547f5eac006b1da5d3b2ddc19daf8564da (patch) | |
tree | 3631abc791106df611ddeee8a0c27f0a90b72ae0 /common | |
parent | bf8459dc1b06f4a1f7e36e30fbe32136ab9c5a25 (diff) | |
parent | 1f45bb1e3b2d9ca54b19fc5c150aa13ba9df236f (diff) |
Merge "added in graceful shutdown to spring boot"
Diffstat (limited to 'common')
-rw-r--r-- | common/pom.xml | 5 | ||||
-rw-r--r-- | common/src/main/java/org/onap/so/spring/GracefulShutdown.java | 41 | ||||
-rw-r--r-- | common/src/main/java/org/onap/so/spring/GracefulShutdownBeans.java | 20 |
3 files changed, 66 insertions, 0 deletions
diff --git a/common/pom.xml b/common/pom.xml index 529841da72..04deacf129 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -97,6 +97,11 @@ </exclusions> </dependency> <dependency> + <groupId>org.apache.tomcat</groupId> + <artifactId>tomcat-catalina</artifactId> + <version>9.0.30</version> + </dependency> + <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-ext</artifactId> </dependency> diff --git a/common/src/main/java/org/onap/so/spring/GracefulShutdown.java b/common/src/main/java/org/onap/so/spring/GracefulShutdown.java new file mode 100644 index 0000000000..60f5b076cd --- /dev/null +++ b/common/src/main/java/org/onap/so/spring/GracefulShutdown.java @@ -0,0 +1,41 @@ +package org.onap.so.spring; + +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import org.apache.catalina.connector.Connector; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer; +import org.springframework.context.event.ContextClosedEvent; +import org.springframework.context.event.EventListener; + +public class GracefulShutdown implements TomcatConnectorCustomizer { + + private static final Logger logger = LoggerFactory.getLogger(GracefulShutdown.class); + + private volatile Connector connector; + + @Override + public void customize(Connector connector) { + this.connector = connector; + } + + @EventListener + public void handleClosedEvent(ContextClosedEvent event) { + this.connector.pause(); + Executor executor = this.connector.getProtocolHandler().getExecutor(); + if (executor instanceof ThreadPoolExecutor) { + try { + ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor; + threadPoolExecutor.shutdown(); + if (!threadPoolExecutor.awaitTermination(30, TimeUnit.SECONDS)) { + logger.warn("Tomcat thread pool did not shut down gracefully within " + + "30 seconds. Proceeding with forceful shutdown"); + } + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } + } +} diff --git a/common/src/main/java/org/onap/so/spring/GracefulShutdownBeans.java b/common/src/main/java/org/onap/so/spring/GracefulShutdownBeans.java new file mode 100644 index 0000000000..219c82fb19 --- /dev/null +++ b/common/src/main/java/org/onap/so/spring/GracefulShutdownBeans.java @@ -0,0 +1,20 @@ +package org.onap.so.spring; + +import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; +import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; +import org.springframework.context.annotation.Bean; + +public class GracefulShutdownBeans { + + @Bean + public GracefulShutdown gracefulShutdown() { + return new GracefulShutdown(); + } + + @Bean + public ConfigurableServletWebServerFactory webServerFactory(final GracefulShutdown gracefulShutdown) { + TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); + factory.addConnectorCustomizers(gracefulShutdown); + return factory; + } +} |