You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by me...@apache.org on 2019/12/18 08:17:36 UTC

[dubbo] branch 2.7.5-release updated: coordinate spring and dubbo shutdown hook. (#5504)

This is an automated email from the ASF dual-hosted git repository.

mercyblitz pushed a commit to branch 2.7.5-release
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/2.7.5-release by this push:
     new 39ffc17  coordinate spring and dubbo shutdown hook. (#5504)
39ffc17 is described below

commit 39ffc17676a054fbe4a8492898ed27aeb8983c38
Author: ken.lj <ke...@gmail.com>
AuthorDate: Wed Dec 18 16:17:21 2019 +0800

    coordinate spring and dubbo shutdown hook. (#5504)
---
 .../org/apache/dubbo/config/DubboShutdownHook.java |  6 +--
 .../dubbo/config/bootstrap/DubboBootstrap.java     | 10 ++--
 .../spring/extension/SpringExtensionFactory.java   | 18 -------
 .../spring/util/ApplicationContextUtils.java       | 60 ----------------------
 .../spring/util/ApplicationContextUtilsTest.java   | 46 -----------------
 5 files changed, 8 insertions(+), 132 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 8fc2717..b35b8b9 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
@@ -25,7 +25,6 @@ import org.apache.dubbo.config.event.DubboShutdownHookRegisteredEvent;
 import org.apache.dubbo.config.event.DubboShutdownHookUnregisteredEvent;
 import org.apache.dubbo.event.Event;
 import org.apache.dubbo.event.EventDispatcher;
-import org.apache.dubbo.registry.support.AbstractRegistryFactory;
 import org.apache.dubbo.rpc.Protocol;
 
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -113,10 +112,7 @@ public class DubboShutdownHook extends Thread {
         if (!destroyed.compareAndSet(false, true)) {
             return;
         }
-        // destroy all the registries
-        AbstractRegistryFactory.destroyAll();
-        // destroy all the protocols
-        destroyProtocols();
+
         // dispatch the DubboDestroyedEvent @since 2.7.5
         dispatch(new DubboServiceDestroyedEvent(this));
     }
diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/bootstrap/DubboBootstrap.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/bootstrap/DubboBootstrap.java
index ecdd8cb..a6cbd53 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/bootstrap/DubboBootstrap.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/bootstrap/DubboBootstrap.java
@@ -155,6 +155,8 @@ public class DubboBootstrap extends GenericEventListener {
 
     private AtomicBoolean started = new AtomicBoolean(false);
 
+    private AtomicBoolean destroyed = new AtomicBoolean(false);
+
     private volatile ServiceInstance serviceInstance;
 
     private volatile MetadataService metadataService;
@@ -181,6 +183,7 @@ public class DubboBootstrap extends GenericEventListener {
         configManager = ApplicationModel.getConfigManager();
         environment = ApplicationModel.getEnvironment();
 
+        DubboShutdownHook.getDubboShutdownHook().register();
         ShutdownHookCallbacks.INSTANCE.addCallback(new ShutdownHookCallback() {
             @Override
             public void callback() throws Throwable {
@@ -189,8 +192,8 @@ public class DubboBootstrap extends GenericEventListener {
         });
     }
 
-    public void registerShutdownHook() {
-        DubboShutdownHook.getDubboShutdownHook().register();
+    public void unRegisterShutdownHook() {
+        DubboShutdownHook.getDubboShutdownHook().unregister();
     }
 
     private boolean isOnlyRegisterProvider() {
@@ -987,7 +990,8 @@ public class DubboBootstrap extends GenericEventListener {
     }
 
     public void destroy() {
-        if (started.compareAndSet(true, false)) {
+        if (started.compareAndSet(true, false)
+                && destroyed.compareAndSet(false, true)) {
             unregisterServiceInstance();
             unexportMetadataService();
             unexportServices();
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 e0f360e..a35645d 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
@@ -21,15 +21,10 @@ import org.apache.dubbo.common.extension.SPI;
 import org.apache.dubbo.common.logger.Logger;
 import org.apache.dubbo.common.logger.LoggerFactory;
 import org.apache.dubbo.common.utils.ConcurrentHashSet;
-import org.apache.dubbo.config.DubboShutdownHook;
-import org.apache.dubbo.config.spring.util.ApplicationContextUtils;
 
 import com.alibaba.spring.util.BeanFactoryUtils;
 import org.springframework.context.ApplicationContext;
-import org.springframework.context.ApplicationEvent;
-import org.springframework.context.ApplicationListener;
 import org.springframework.context.ConfigurableApplicationContext;
-import org.springframework.context.event.ContextClosedEvent;
 
 import java.util.Set;
 
@@ -40,15 +35,12 @@ public class SpringExtensionFactory implements ExtensionFactory {
     private static final Logger logger = LoggerFactory.getLogger(SpringExtensionFactory.class);
 
     private static final Set<ApplicationContext> CONTEXTS = new ConcurrentHashSet<ApplicationContext>();
-    private static final ApplicationListener SHUTDOWN_HOOK_LISTENER = new ShutdownHookListener();
 
     public static void addApplicationContext(ApplicationContext context) {
         CONTEXTS.add(context);
         if (context instanceof ConfigurableApplicationContext) {
             ((ConfigurableApplicationContext) context).registerShutdownHook();
-            DubboShutdownHook.getDubboShutdownHook().unregister();
         }
-        ApplicationContextUtils.addApplicationListener(context, SHUTDOWN_HOOK_LISTENER);
     }
 
     public static void removeApplicationContext(ApplicationContext context) {
@@ -84,14 +76,4 @@ public class SpringExtensionFactory implements ExtensionFactory {
 
         return null;
     }
-
-    private static class ShutdownHookListener implements ApplicationListener {
-        @Override
-        public void onApplicationEvent(ApplicationEvent event) {
-            if (event instanceof ContextClosedEvent) {
-                DubboShutdownHook shutdownHook = DubboShutdownHook.getDubboShutdownHook();
-                shutdownHook.doDestroy();
-            }
-        }
-    }
 }
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ApplicationContextUtils.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ApplicationContextUtils.java
deleted file mode 100644
index 72d1e1c..0000000
--- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/ApplicationContextUtils.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.dubbo.config.spring.util;
-
-import org.springframework.beans.factory.BeanFactory;
-import org.springframework.beans.factory.config.ConfigurableBeanFactory;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.ApplicationListener;
-import org.springframework.context.support.AbstractApplicationContext;
-
-import java.lang.reflect.Method;
-
-/**
- * {@link BeanFactory} Utilities class
- *
- * @see BeanFactory
- * @see ConfigurableBeanFactory
- * @see org.springframework.beans.factory.BeanFactoryUtils
- * @since 2.5.7
- */
-public class ApplicationContextUtils {
-
-    public static boolean addApplicationListener(ApplicationContext applicationContext, ApplicationListener listener) {
-        try {
-            // backward compatibility to spring 2.0.1
-            Method method = applicationContext.getClass().getMethod("addApplicationListener", ApplicationListener.class);
-            method.invoke(applicationContext, listener);
-            return true;
-        } catch (Throwable t) {
-            if (applicationContext instanceof AbstractApplicationContext) {
-                try {
-                    // backward compatibility to spring 2.0.1
-                    Method method = AbstractApplicationContext.class.getDeclaredMethod("addListener", ApplicationListener.class);
-                    if (!method.isAccessible()) {
-                        method.setAccessible(true);
-                    }
-                    method.invoke(applicationContext, listener);
-                    return true;
-                } catch (Throwable t2) {
-                    // ignore
-                }
-            }
-        }
-        return false;
-    }
-}
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/ApplicationContextUtilsTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/ApplicationContextUtilsTest.java
deleted file mode 100644
index b7ebb00..0000000
--- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/ApplicationContextUtilsTest.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.dubbo.config.spring.util;
-
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.support.GenericApplicationContext;
-
-import static org.apache.dubbo.config.spring.util.ApplicationContextUtils.addApplicationListener;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-
-/**
- * {@link ApplicationContextUtils} Test
- *
- * @since 2.5.7
- */
-public class ApplicationContextUtilsTest {
-
-    private ApplicationContext applicationContext;
-
-    @BeforeEach
-    public void init() {
-        applicationContext = new GenericApplicationContext();
-    }
-
-    @Test
-    public void testAddApplicationListener() {
-        assertTrue(addApplicationListener(applicationContext, event -> {
-        }));
-    }
-}