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 2021/02/07 02:51:44 UTC

[dubbo] branch master updated: Introcduce DubboBootstrap start/stop listener(#7159)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new d70b151  Introcduce DubboBootstrap start/stop listener(#7159)
d70b151 is described below

commit d70b151e39839764ce648ca1774ed8e1c2bd3099
Author: TheoneFx <ch...@gmail.com>
AuthorDate: Sun Feb 7 10:51:23 2021 +0800

    Introcduce DubboBootstrap start/stop listener(#7159)
---
 .../dubbo/config/bootstrap/DubboBootstrap.java     |  7 +++
 .../bootstrap/DubboBootstrapStartStopListener.java | 34 +++++++++++++++
 .../context/DubboBootstrapApplicationListener.java |  1 +
 ...bboBootstrapStartStopListenerSpringAdapter.java | 48 +++++++++++++++++++++
 .../context/event/DubboBootstrapStatedEvent.java   | 50 ++++++++++++++++++++++
 .../context/event/DubboBootstrapStopedEvent.java   | 50 ++++++++++++++++++++++
 ...onfig.bootstrap.DubboBootstrapStartStopListener |  1 +
 7 files changed, 191 insertions(+)

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 166fadf..0f9dcbf 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
@@ -915,12 +915,16 @@ public class DubboBootstrap extends GenericEventListener {
                     if (logger.isInfoEnabled()) {
                         logger.info(NAME + " is ready.");
                     }
+                    ExtensionLoader<DubboBootstrapStartStopListener> exts = getExtensionLoader(DubboBootstrapStartStopListener.class);
+                    exts.getSupportedExtensionInstances().forEach(ext -> ext.onStart(this));
                 }).start();
             } else {
                 ready.set(true);
                 if (logger.isInfoEnabled()) {
                     logger.info(NAME + " is ready.");
                 }
+                ExtensionLoader<DubboBootstrapStartStopListener> exts = getExtensionLoader(DubboBootstrapStartStopListener.class);
+                exts.getSupportedExtensionInstances().forEach(ext -> ext.onStart(this));
             }
             if (logger.isInfoEnabled()) {
                 logger.info(NAME + " has started.");
@@ -986,6 +990,7 @@ public class DubboBootstrap extends GenericEventListener {
         return ready.get();
     }
 
+
     public DubboBootstrap stop() throws IllegalStateException {
         destroy();
         return this;
@@ -1268,6 +1273,8 @@ public class DubboBootstrap extends GenericEventListener {
                     clear();
                     shutdown();
                     release();
+                    ExtensionLoader<DubboBootstrapStartStopListener> exts = getExtensionLoader(DubboBootstrapStartStopListener.class);
+                    exts.getSupportedExtensionInstances().forEach(ext -> ext.onStop(this));
                 }
             } finally {
                 destroyLock.unlock();
diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/bootstrap/DubboBootstrapStartStopListener.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/bootstrap/DubboBootstrapStartStopListener.java
new file mode 100644
index 0000000..dc7818d
--- /dev/null
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/bootstrap/DubboBootstrapStartStopListener.java
@@ -0,0 +1,34 @@
+/*
+ * 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.bootstrap;
+
+import org.apache.dubbo.common.extension.SPI;
+
+/**
+ * call on DubboBootstrap start or stop.
+ *
+ * @scene 2.7.9
+ * @see DubboBootstrap
+ */
+@SPI
+public interface DubboBootstrapStartStopListener {
+
+    void onStart(DubboBootstrap bootstrap);
+
+    void onStop(DubboBootstrap bootstrap);
+}
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboBootstrapApplicationListener.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboBootstrapApplicationListener.java
index fcb7208..58d8f40 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboBootstrapApplicationListener.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboBootstrapApplicationListener.java
@@ -46,6 +46,7 @@ public class DubboBootstrapApplicationListener extends OnceApplicationContextEve
     public DubboBootstrapApplicationListener(ApplicationContext applicationContext) {
         super(applicationContext);
         this.dubboBootstrap = DubboBootstrap.getInstance();
+        DubboBootstrapStartStopListenerSpringAdapter.applicationContext = applicationContext;
     }
 
     @Override
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboBootstrapStartStopListenerSpringAdapter.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboBootstrapStartStopListenerSpringAdapter.java
new file mode 100644
index 0000000..3ae3dcf
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboBootstrapStartStopListenerSpringAdapter.java
@@ -0,0 +1,48 @@
+/*
+ * 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.context;
+
+import org.apache.dubbo.config.bootstrap.DubboBootstrap;
+import org.apache.dubbo.config.bootstrap.DubboBootstrapStartStopListener;
+import org.apache.dubbo.config.spring.context.event.DubboBootstrapStatedEvent;
+import org.apache.dubbo.config.spring.context.event.DubboBootstrapStopedEvent;
+
+import org.springframework.context.ApplicationContext;
+
+/**
+ * convcert Dubbo bootstrap event to spring environment.
+ *
+ * @scene 2.7.9
+ */
+public class DubboBootstrapStartStopListenerSpringAdapter implements DubboBootstrapStartStopListener {
+
+    static ApplicationContext applicationContext;
+
+    @Override
+    public void onStart(DubboBootstrap bootstrap) {
+        if (applicationContext != null) {
+            applicationContext.publishEvent(new DubboBootstrapStatedEvent(bootstrap));
+        }
+    }
+
+    @Override
+    public void onStop(DubboBootstrap bootstrap) {
+        if (applicationContext != null) {
+            applicationContext.publishEvent(new DubboBootstrapStopedEvent(bootstrap));
+        }
+    }
+}
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/event/DubboBootstrapStatedEvent.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/event/DubboBootstrapStatedEvent.java
new file mode 100644
index 0000000..0ebaa43
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/event/DubboBootstrapStatedEvent.java
@@ -0,0 +1,50 @@
+/*
+ * 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.context.event;
+
+import org.springframework.context.ApplicationEvent;
+
+import org.apache.dubbo.config.bootstrap.DubboBootstrap;
+
+/**
+ * A {@link org.springframework.context.ApplicationEvent} after {@link org.apache.dubbo.config.bootstrap.DubboBootstrap#start()} success
+ *
+ * @see org.springframework.context.ApplicationEvent
+ * @see org.springframework.context.ApplicationListener
+ * @see org.apache.dubbo.config.bootstrap.DubboBootstrap
+ * @since 2.7.9
+ */
+public class DubboBootstrapStatedEvent extends ApplicationEvent {
+
+    /**
+     * Create a new ApplicationEvent.
+     *
+     * @param bootstrap {@link org.apache.dubbo.config.bootstrap.DubboBootstrap} bootstrap
+     */
+    public DubboBootstrapStatedEvent(DubboBootstrap bootstrap) {
+        super(bootstrap);
+    }
+
+    /**
+     * Get {@link org.apache.dubbo.config.bootstrap.DubboBootstrap} instance
+     *
+     * @return non-null
+     */
+    public DubboBootstrap getDubboBootstrap() {
+        return (DubboBootstrap) super.getSource();
+    }
+}
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/event/DubboBootstrapStopedEvent.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/event/DubboBootstrapStopedEvent.java
new file mode 100644
index 0000000..ced058b
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/event/DubboBootstrapStopedEvent.java
@@ -0,0 +1,50 @@
+/*
+ * 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.context.event;
+
+import org.springframework.context.ApplicationEvent;
+
+import org.apache.dubbo.config.bootstrap.DubboBootstrap;
+
+/**
+ * A {@link org.springframework.context.ApplicationEvent} after {@link org.apache.dubbo.config.bootstrap.DubboBootstrap#stop()} success
+ *
+ * @see org.springframework.context.ApplicationEvent
+ * @see org.springframework.context.ApplicationListener
+ * @see org.apache.dubbo.config.bootstrap.DubboBootstrap
+ * @since 2.7.9
+ */
+public class DubboBootstrapStopedEvent extends ApplicationEvent {
+
+    /**
+     * Create a new ApplicationEvent.
+     *
+     * @param bootstrap {@link org.apache.dubbo.config.bootstrap.DubboBootstrap} bootstrap
+     */
+    public DubboBootstrapStopedEvent(DubboBootstrap bootstrap) {
+        super(bootstrap);
+    }
+
+    /**
+     * Get {@link org.apache.dubbo.config.bootstrap.DubboBootstrap} instance
+     *
+     * @return non-null
+     */
+    public DubboBootstrap getDubboBootstrap() {
+        return (DubboBootstrap) super.getSource();
+    }
+}
diff --git a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.config.bootstrap.DubboBootstrapStartStopListener b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.config.bootstrap.DubboBootstrapStartStopListener
new file mode 100644
index 0000000..8775af2
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.config.bootstrap.DubboBootstrapStartStopListener
@@ -0,0 +1 @@
+default=org.apache.dubbo.config.spring.context.DubboBootstrapStartStopListenerSpringAdapter
\ No newline at end of file