From f6a32137354091b417aef45c7aac82dd36b2aad5 Mon Sep 17 00:00:00 2001 From: lizhen Date: Thu, 6 Dec 2018 19:14:40 +0800 Subject: [PATCH] Graceful shutdown enhancement in Spring --- .../java/org/apache/dubbo/config/DubboShutdownHook.java | 4 ++-- .../config/spring/extension/SpringExtensionFactory.java | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/DubboShutdownHook.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/DubboShutdownHook.java index 90ed2376cd5..d554281f031 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/DubboShutdownHook.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/DubboShutdownHook.java @@ -55,13 +55,13 @@ public void run() { if (logger.isInfoEnabled()) { logger.info("Run shutdown hook now."); } - destroyAll(); + doDestroy(); } /** * Destroy all the resources, including registries and protocols. */ - public void destroyAll() { + public void doDestroy() { if (!destroyed.compareAndSet(false, true)) { return; } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactory.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactory.java index 99b538c25e0..755db1a8492 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactory.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/extension/SpringExtensionFactory.java @@ -44,6 +44,7 @@ public class SpringExtensionFactory implements ExtensionFactory { public static void addApplicationContext(ApplicationContext context) { contexts.add(context); + Runtime.getRuntime().removeShutdownHook(DubboShutdownHook.getDubboShutdownHook()); BeanFactoryUtils.addApplicationListener(context, shutdownHookListener); } @@ -105,11 +106,8 @@ private static class ShutdownHookListener implements ApplicationListener { @Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ContextClosedEvent) { - // we call it anyway since dubbo shutdown hook make sure its destroyAll() is re-entrant. - // pls. note we should not remove dubbo shutdown hook when spring framework is present, this is because - // its shutdown hook may not be installed. DubboShutdownHook shutdownHook = DubboShutdownHook.getDubboShutdownHook(); - shutdownHook.destroyAll(); + shutdownHook.doDestroy(); } } }