You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by xi...@apache.org on 2020/12/22 04:34:27 UTC

[shardingsphere] branch master updated: refactor: divide services lifecycle to a separated class (#8710)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new cfc4abe  refactor: divide services lifecycle to a separated class (#8710)
cfc4abe is described below

commit cfc4abe526dff1edb486ca0c938b86172a054c40
Author: Daming <zt...@foxmail.com>
AuthorDate: Tue Dec 22 12:34:05 2020 +0800

    refactor: divide services lifecycle to a separated class (#8710)
---
 .../agent/bootstrap/ShardingSphereAgent.java       | 12 ++--
 .../agent/core/plugin/AgentPluginLoader.java       | 56 +++------------
 .../agent/core/plugin/ServiceSupervisor.java       | 79 ++++++++++++++++++++++
 3 files changed, 97 insertions(+), 50 deletions(-)

diff --git a/shardingsphere-agent/shardingsphere-agent-bootstrap/src/main/java/org/apache/shardingsphere/agent/bootstrap/ShardingSphereAgent.java b/shardingsphere-agent/shardingsphere-agent-bootstrap/src/main/java/org/apache/shardingsphere/agent/bootstrap/ShardingSphereAgent.java
index 75b2d42..10880aa 100644
--- a/shardingsphere-agent/shardingsphere-agent-bootstrap/src/main/java/org/apache/shardingsphere/agent/bootstrap/ShardingSphereAgent.java
+++ b/shardingsphere-agent/shardingsphere-agent-bootstrap/src/main/java/org/apache/shardingsphere/agent/bootstrap/ShardingSphereAgent.java
@@ -13,7 +13,6 @@
  * 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.shardingsphere.agent.bootstrap;
@@ -26,6 +25,7 @@ import org.apache.shardingsphere.agent.core.LoggingListener;
 import org.apache.shardingsphere.agent.core.ShardingSphereTransformer;
 import org.apache.shardingsphere.agent.core.config.AgentConfigurationLoader;
 import org.apache.shardingsphere.agent.core.plugin.AgentPluginLoader;
+import org.apache.shardingsphere.agent.core.plugin.ServiceSupervisor;
 import org.apache.shardingsphere.agent.core.utils.SingletonHolder;
 
 import java.io.IOException;
@@ -47,17 +47,21 @@ public class ShardingSphereAgent {
         SingletonHolder.INSTANCE.put(AgentConfigurationLoader.load());
         AgentPluginLoader agentPluginLoader = createAgentPluginLoader();
         setUpAgentBuilder(instrumentation, agentPluginLoader);
-        Runtime.getRuntime().addShutdownHook(new Thread(agentPluginLoader::shutdownAllServices));
+        superviseServices(agentPluginLoader.getServices());
     }
     
     private static AgentPluginLoader createAgentPluginLoader() throws IOException {
         AgentPluginLoader result = AgentPluginLoader.getInstance();
         result.loadAllPlugins();
-        result.initialAllServices();
-        result.startAllServices();
         return result;
     }
     
+    private static void superviseServices(final ServiceSupervisor serviceSupervisor) {
+        serviceSupervisor.setUpAllServices();
+        serviceSupervisor.startAllServices();
+        Runtime.getRuntime().addShutdownHook(new Thread(serviceSupervisor::cleanUpAllServices));
+    }
+    
     private static void setUpAgentBuilder(final Instrumentation instrumentation, final AgentPluginLoader agentPluginLoader) {
         AgentBuilder agentBuilder = new AgentBuilder.Default().with(new ByteBuddy().with(TypeValidation.ENABLED))
                 .ignore(ElementMatchers.isSynthetic()).or(ElementMatchers.nameStartsWith("org.apache.shardingsphere.agent."));
diff --git a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/AgentPluginLoader.java b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/AgentPluginLoader.java
index 786cc6e..fccc12e 100644
--- a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/AgentPluginLoader.java
+++ b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/AgentPluginLoader.java
@@ -13,12 +13,12 @@
  * 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.shardingsphere.agent.core.plugin;
 
 import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
@@ -159,51 +159,6 @@ public final class AgentPluginLoader extends ClassLoader implements Closeable {
     }
     
     /**
-     * Initial all services.
-     */
-    public void initialAllServices() {
-        services.forEach(service -> {
-            try {
-                service.setup();
-                // CHECKSTYLE:OFF
-            } catch (final Throwable ex) {
-                // CHECKSTYLE:ON
-                log.error("Failed to initial service.", ex);
-            }
-        });
-    }
-    
-    /**
-     * Start all services.
-     */
-    public void startAllServices() {
-        services.forEach(service -> {
-            try {
-                service.start();
-                // CHECKSTYLE:OFF
-            } catch (final Throwable ex) {
-                // CHECKSTYLE:ON
-                log.error("Failed to start service.", ex);
-            }
-        });
-    }
-    
-    /**
-     * Shutdown all services.
-     */
-    public void shutdownAllServices() {
-        services.forEach(service -> {
-            try {
-                service.cleanup();
-                // CHECKSTYLE:OFF
-            } catch (final Throwable ex) {
-                // CHECKSTYLE:ON
-                log.error("Failed to shutdown service.", ex);
-            }
-        });
-    }
-    
-    /**
      * To find all intercepting target classes then to build TypeMatcher.
      *
      * @return type matcher
@@ -249,6 +204,15 @@ public final class AgentPluginLoader extends ClassLoader implements Closeable {
     }
     
     /**
+     * Get the supervisor of services.
+     *
+     * @return service supervisor
+     */
+    public ServiceSupervisor getServices() {
+        return new ServiceSupervisor(ImmutableList.<Service>builder().addAll(services).build());
+    }
+    
+    /**
      * To get or create instance of the advice class. Create new one and caching when it is not exist.
      *
      * @param classNameOfAdvice class name of advice
diff --git a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/ServiceSupervisor.java b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/ServiceSupervisor.java
new file mode 100644
index 0000000..c357241
--- /dev/null
+++ b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/ServiceSupervisor.java
@@ -0,0 +1,79 @@
+/*
+ * 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.shardingsphere.agent.core.plugin;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.List;
+
+@Slf4j
+public final class ServiceSupervisor {
+    
+    private final List<Service> services;
+    
+    public ServiceSupervisor(final List<Service> services) {
+        this.services = services;
+    }
+    
+    
+    /**
+     * Initial all services.
+     */
+    public void setUpAllServices() {
+        services.forEach(service -> {
+            try {
+                service.setup();
+                // CHECKSTYLE:OFF
+            } catch (final Throwable ex) {
+                // CHECKSTYLE:ON
+                log.error("Failed to initial service.", ex);
+            }
+        });
+    }
+    
+    /**
+     * Start all services.
+     */
+    public void startAllServices() {
+        services.forEach(service -> {
+            try {
+                service.start();
+                // CHECKSTYLE:OFF
+            } catch (final Throwable ex) {
+                // CHECKSTYLE:ON
+                log.error("Failed to start service.", ex);
+            }
+        });
+    }
+    
+    /**
+     * Shutdown all services.
+     */
+    public void cleanUpAllServices() {
+        services.forEach(service -> {
+            try {
+                service.cleanup();
+                // CHECKSTYLE:OFF
+            } catch (final Throwable ex) {
+                // CHECKSTYLE:ON
+                log.error("Failed to shutdown service.", ex);
+            }
+        });
+    }
+    
+}