diff options
author | Kalkere Ramesh, Sharan <sharan.kalkere.ramesh@att.com> | 2020-02-10 11:11:04 -0500 |
---|---|---|
committer | Benjamin, Max (mb388a) <mb388a@att.com> | 2020-02-10 11:11:04 -0500 |
commit | 1f45bb1e3b2d9ca54b19fc5c150aa13ba9df236f (patch) | |
tree | 15ef0a10db46a4544b684a21c5c986293292172b | |
parent | ac1a59fc6731c6261712eac5bec76816b11d1096 (diff) |
added in graceful shutdown to spring boot
added in graceful shutdown to spring boot
updated the poms to include catalina and upped fabric
added in eventlistener instead of application listener
Issue-ID: SO-2655
Signed-off-by: Benjamin, Max (mb388a) <mb388a@att.com>
Change-Id: I2d8674cc82a9167604b2d38a49bffba42cee32ff
-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; + } +} |