liubao68 closed pull request #878: [SCB-854] BootListener support load by not 
only spring bean, but also SPI
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/878
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/core/src/main/java/org/apache/servicecomb/core/BootListener.java 
b/core/src/main/java/org/apache/servicecomb/core/BootListener.java
index b3b875543..848ccdc31 100644
--- a/core/src/main/java/org/apache/servicecomb/core/BootListener.java
+++ b/core/src/main/java/org/apache/servicecomb/core/BootListener.java
@@ -45,5 +45,9 @@ public void setEventType(EventType eventType) {
     }
   }
 
+  default int getOrder() {
+    return 0;
+  }
+
   void onBootEvent(BootEvent event);
 }
diff --git a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java 
b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
index 1bc46c94e..dcd877f40 100644
--- a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
+++ b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
@@ -16,7 +16,10 @@
  */
 package org.apache.servicecomb.core;
 
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Comparator;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicLong;
 
@@ -36,6 +39,7 @@
 import org.apache.servicecomb.core.provider.producer.ProducerProviderManager;
 import org.apache.servicecomb.core.transport.TransportManager;
 import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
 import org.apache.servicecomb.foundation.vertx.VertxUtils;
 import org.apache.servicecomb.serviceregistry.RegistryUtils;
 import 
org.apache.servicecomb.serviceregistry.task.MicroserviceInstanceRegisterTask;
@@ -70,7 +74,6 @@
 
   private volatile SCBStatus status = SCBStatus.DOWN;
 
-
   public void setStatus(SCBStatus status) {
     this.status = status;
   }
@@ -115,7 +118,12 @@ public void setSchemaListenerManager(
   }
 
   public void setBootListenerList(Collection<BootListener> bootListenerList) {
-    this.bootListenerList = bootListenerList;
+    List<BootListener> tmp = new ArrayList<>();
+    tmp.addAll(bootListenerList);
+    tmp.addAll(SPIServiceUtils.getOrLoadSortedService(BootListener.class));
+    tmp.sort(Comparator.comparingInt(BootListener::getOrder));
+
+    this.bootListenerList = tmp;
   }
 
   protected void triggerEvent(EventType eventType) {
diff --git a/core/src/test/java/org/apache/servicecomb/core/TestSCBEngine.java 
b/core/src/test/java/org/apache/servicecomb/core/TestSCBEngine.java
index 177a46bb3..ea6371b46 100644
--- a/core/src/test/java/org/apache/servicecomb/core/TestSCBEngine.java
+++ b/core/src/test/java/org/apache/servicecomb/core/TestSCBEngine.java
@@ -18,6 +18,7 @@
 package org.apache.servicecomb.core;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.servicecomb.config.ConfigUtil;
@@ -26,6 +27,7 @@
 import org.apache.servicecomb.core.provider.consumer.ReferenceConfig;
 import org.apache.servicecomb.core.provider.producer.ProducerProviderManager;
 import org.apache.servicecomb.core.transport.TransportManager;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
 import org.apache.servicecomb.foundation.vertx.VertxUtils;
 import org.apache.servicecomb.serviceregistry.RegistryUtils;
 import org.apache.servicecomb.serviceregistry.consumer.AppManager;
@@ -134,4 +136,23 @@ public void getReferenceConfigForInvoke_down(@Mocked 
ConsumerProviderManager con
             .is("InvocationException: code=503;msg=CommonExceptionData 
[message=The request is rejected. Cannot process the request due to STATUS = 
DOWN]"));
     engine.getReferenceConfigForInvoke(null);
   }
+
+  @Test
+  public void setBootListenerList(@Mocked BootListener beanListener, @Mocked 
BootListener spiListener) {
+    new Expectations(SPIServiceUtils.class) {
+      {
+        beanListener.getOrder();
+        result = 1;
+        spiListener.getOrder();
+        result = 0;
+        SPIServiceUtils.getOrLoadSortedService(BootListener.class);
+        result = Arrays.asList(spiListener);
+      }
+    };
+
+    SCBEngine engine = new SCBEngine();
+    engine.setBootListenerList(Arrays.asList(beanListener));
+
+    Assert.assertThat(engine.getBootListenerList(), 
Matchers.contains(spiListener, beanListener));
+  }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to