diff options
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; + } +} |