diff options
Diffstat (limited to 'common/src/main/java/org')
-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 |
2 files changed, 61 insertions, 0 deletions
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; + } +} |