You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2023/09/01 05:32:26 UTC

[dubbo] branch 3.2 updated: Ignore start if scope model is LifeCycleManagedExternally (#12985)

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

liujun pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.2 by this push:
     new bfa46f59eb Ignore start if scope model is LifeCycleManagedExternally (#12985)
bfa46f59eb is described below

commit bfa46f59eb0054f86e6326f92a1c69cc40eeb58e
Author: Albumen Kevin <jh...@gmail.com>
AuthorDate: Fri Sep 1 13:32:19 2023 +0800

    Ignore start if scope model is LifeCycleManagedExternally (#12985)
---
 .../org/apache/dubbo/config/ReferenceConfig.java   |  9 ++++++--
 .../org/apache/dubbo/config/ServiceConfig.java     |  9 ++++++--
 .../apache/dubbo/config/spring/ReferenceBean.java  | 11 ++++++++--
 .../context/DubboConfigApplicationListener.java    | 25 +++++++++++++---------
 .../spring/reference/ReferenceBeanManager.java     |  2 +-
 5 files changed, 39 insertions(+), 17 deletions(-)

diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java
index f3284c16c6..ac67102c68 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java
@@ -227,8 +227,13 @@ public class ReferenceConfig<T> extends ReferenceConfigBase<T> {
         }
 
         if (ref == null) {
-            // ensure start module, compatible with old api usage
-            getScopeModel().getDeployer().start();
+            if (getScopeModel().isLifeCycleManagedExternally()) {
+                // prepare model for reference
+                getScopeModel().getDeployer().prepare();
+            } else {
+                // ensure start module, compatible with old api usage
+                getScopeModel().getDeployer().start();
+            }
 
             init(check);
         }
diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
index 2c8d39c4f1..7d14934057 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
@@ -295,8 +295,13 @@ public class ServiceConfig<T> extends ServiceConfigBase<T> {
             return;
         }
 
-        // ensure start module, compatible with old api usage
-        getScopeModel().getDeployer().start();
+        if (getScopeModel().isLifeCycleManagedExternally()) {
+            // prepare model for reference
+            getScopeModel().getDeployer().prepare();
+        } else {
+            // ensure start module, compatible with old api usage
+            getScopeModel().getDeployer().start();
+        }
 
         synchronized (this) {
             if (this.exported) {
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java
index 46e91f61f3..30790d3fd8 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java
@@ -24,6 +24,7 @@ import org.apache.dubbo.common.utils.Assert;
 import org.apache.dubbo.common.utils.ClassUtils;
 import org.apache.dubbo.common.utils.StringUtils;
 import org.apache.dubbo.config.ReferenceConfig;
+import org.apache.dubbo.config.spring.context.DubboConfigApplicationListener;
 import org.apache.dubbo.config.spring.context.DubboConfigBeanInitializer;
 import org.apache.dubbo.config.spring.reference.ReferenceAttributes;
 import org.apache.dubbo.config.spring.reference.ReferenceBeanManager;
@@ -53,6 +54,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
+import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_DUBBO_BEAN_INITIALIZER;
 import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROXY_FAILED;
 
 
@@ -143,6 +145,9 @@ public class ReferenceBean<T> implements FactoryBean<T>,
     //actual reference config
     private ReferenceConfig referenceConfig;
 
+    // ReferenceBeanManager
+    private ReferenceBeanManager referenceBeanManager;
+
     // Registration sources of this reference, may be xml file or annotation location
     private List<Map<String,Object>> sources = new ArrayList<>();
 
@@ -251,7 +256,7 @@ public class ReferenceBean<T> implements FactoryBean<T>,
         }
         Assert.notNull(this.interfaceName, "The interface name of ReferenceBean is not initialized");
 
-        ReferenceBeanManager referenceBeanManager = beanFactory.getBean(ReferenceBeanManager.BEAN_NAME, ReferenceBeanManager.class);
+        this.referenceBeanManager = beanFactory.getBean(ReferenceBeanManager.BEAN_NAME, ReferenceBeanManager.class);
         referenceBeanManager.addReference(this);
     }
 
@@ -387,7 +392,9 @@ public class ReferenceBean<T> implements FactoryBean<T>,
 
     private Object getCallProxy() throws Exception {
         if (referenceConfig == null) {
-            throw new IllegalStateException("ReferenceBean is not ready yet, please make sure to call reference interface method after dubbo is started.");
+            referenceBeanManager.initReferenceBean(this);
+            applicationContext.getBean(DubboConfigApplicationListener.class.getName(), DubboConfigApplicationListener.class).init();
+            logger.warn(CONFIG_DUBBO_BEAN_INITIALIZER, "", "",  "ReferenceBean is not ready yet, please make sure to call reference interface method after dubbo is started.");
         }
         //get reference proxy
         //Subclasses should synchronize on the given Object if they perform any sort of extended singleton creation phase.
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboConfigApplicationListener.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboConfigApplicationListener.java
index 17343637bb..2cdf512053 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboConfigApplicationListener.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboConfigApplicationListener.java
@@ -16,21 +16,22 @@
  */
 package org.apache.dubbo.config.spring.context;
 
-import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_DUBBO_BEAN_NOT_FOUND;
-import static org.springframework.util.ObjectUtils.nullSafeEquals;
-
-import java.util.concurrent.atomic.AtomicBoolean;
-
 import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
 import org.apache.dubbo.common.logger.LoggerFactory;
 import org.apache.dubbo.config.spring.context.event.DubboConfigInitEvent;
 import org.apache.dubbo.config.spring.util.DubboBeanUtils;
 import org.apache.dubbo.rpc.model.ModuleModel;
+
 import org.springframework.beans.BeansException;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
 import org.springframework.context.ApplicationListener;
 
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_DUBBO_BEAN_NOT_FOUND;
+import static org.springframework.util.ObjectUtils.nullSafeEquals;
+
 /**
  * An ApplicationListener to load config beans
  */
@@ -53,11 +54,15 @@ public class DubboConfigApplicationListener implements ApplicationListener<Dubbo
     @Override
     public void onApplicationEvent(DubboConfigInitEvent event) {
         if (nullSafeEquals(applicationContext, event.getSource())) {
-            // It's expected to be notified at org.springframework.context.support.AbstractApplicationContext.registerListeners(),
-            // before loading non-lazy singleton beans. At this moment, all BeanFactoryPostProcessor have been processed,
-            if (initialized.compareAndSet(false, true)) {
-                initDubboConfigBeans();
-            }
+            init();
+        }
+    }
+
+    public void init() {
+        // It's expected to be notified at org.springframework.context.support.AbstractApplicationContext.registerListeners(),
+        // before loading non-lazy singleton beans. At this moment, all BeanFactoryPostProcessor have been processed,
+        if (initialized.compareAndSet(false, true)) {
+            initDubboConfigBeans();
         }
     }
 
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/reference/ReferenceBeanManager.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/reference/ReferenceBeanManager.java
index 1ebffa04ea..0e428a8647 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/reference/ReferenceBeanManager.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/reference/ReferenceBeanManager.java
@@ -157,7 +157,7 @@ public class ReferenceBeanManager implements ApplicationContextAware {
      * @param referenceBean
      * @throws Exception
      */
-    private synchronized void initReferenceBean(ReferenceBean referenceBean) throws Exception {
+    public synchronized void initReferenceBean(ReferenceBean referenceBean) throws Exception {
 
         if (referenceBean.getReferenceConfig() != null) {
             return;