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

zhangliang 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 233d142f39c Move ContextManagerLifecycleListener into ContextManager 
(#31692)
233d142f39c is described below

commit 233d142f39ce215c11fa31c30ce6c191cddede2d
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Jun 13 23:42:07 2024 +0800

    Move ContextManagerLifecycleListener into ContextManager (#31692)
---
 .../jdbc/core/datasource/ShardingSphereDataSource.java   | 16 ----------------
 .../shardingsphere/mode/manager/ContextManager.java      |  8 ++++++++
 .../proxy/initializer/BootstrapInitializer.java          | 15 ---------------
 3 files changed, 8 insertions(+), 31 deletions(-)

diff --git 
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
 
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
index 960fee08216..b4c91965a3a 100644
--- 
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
+++ 
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
@@ -27,13 +27,11 @@ import 
org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration
 import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
 import 
org.apache.shardingsphere.infra.instance.metadata.InstanceMetaDataBuilder;
 import 
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
-import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.mode.manager.ContextManagerBuilder;
 import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
-import 
org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListener;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
@@ -63,7 +61,6 @@ public final class ShardingSphereDataSource extends 
AbstractDataSourceAdapter im
                                     final Collection<RuleConfiguration> 
ruleConfigs, final Properties props) throws SQLException {
         this.databaseName = databaseName;
         contextManager = createContextManager(modeConfig, dataSourceMap, 
ruleConfigs, null == props ? new Properties() : props);
-        contextManagerInitializedCallback();
     }
     
     private ContextManager createContextManager(final ModeConfiguration 
modeConfig, final Map<String, DataSource> dataSourceMap,
@@ -77,12 +74,6 @@ public final class ShardingSphereDataSource extends 
AbstractDataSourceAdapter im
         return TypedSPILoader.getService(ContextManagerBuilder.class, null == 
modeConfig ? null : modeConfig.getType()).build(param, new EventBusContext());
     }
     
-    private void contextManagerInitializedCallback() {
-        for (ContextManagerLifecycleListener each : 
ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class))
 {
-            each.onInitialized(contextManager);
-        }
-    }
-    
     @HighFrequencyInvocation(canBeCached = true)
     @Override
     public Connection getConnection() {
@@ -110,7 +101,6 @@ public final class ShardingSphereDataSource extends 
AbstractDataSourceAdapter im
     
     @Override
     public void close() throws SQLException {
-        contextManagerDestroyedCallback();
         for (StorageUnit each : 
contextManager.getStorageUnits(databaseName).values()) {
             close(each.getDataSource());
         }
@@ -128,10 +118,4 @@ public final class ShardingSphereDataSource extends 
AbstractDataSourceAdapter im
             }
         }
     }
-    
-    private void contextManagerDestroyedCallback() {
-        for (ContextManagerLifecycleListener each : 
ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class))
 {
-            each.onDestroyed(contextManager);
-        }
-    }
 }
diff --git 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
index 2cbc5de7474..95b841c2dc4 100644
--- 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
+++ 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
@@ -38,8 +38,10 @@ import 
org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericS
 import 
org.apache.shardingsphere.infra.metadata.database.schema.manager.GenericSchemaManager;
 import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
 import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.infra.state.cluster.ClusterState;
 import org.apache.shardingsphere.metadata.persist.MetaDataPersistService;
+import 
org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListener;
 import org.apache.shardingsphere.mode.manager.switcher.ResourceSwitchManager;
 import org.apache.shardingsphere.mode.manager.switcher.SwitchingResource;
 import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
@@ -85,6 +87,9 @@ public final class ContextManager implements AutoCloseable {
         stateContext = new 
StateContext(persistServiceFacade.getStatePersistService().loadClusterState().orElse(ClusterState.OK));
         metaDataContextManager = new 
MetaDataContextManager(this.metaDataContexts, computeNodeInstanceContext, 
persistServiceFacade);
         executorEngine = 
ExecutorEngine.createExecutorEngineWithSize(metaDataContexts.getMetaData().getProps().<Integer>getValue(ConfigurationPropertyKey.KERNEL_EXECUTOR_SIZE));
+        for (ContextManagerLifecycleListener each : 
ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class))
 {
+            each.onInitialized(this);
+        }
     }
     
     /**
@@ -287,6 +292,9 @@ public final class ContextManager implements AutoCloseable {
     
     @Override
     public void close() {
+        for (ContextManagerLifecycleListener each : 
ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class))
 {
+            each.onDestroyed(this);
+        }
         executorEngine.close();
         metaDataContexts.get().close();
         repository.close();
diff --git 
a/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
 
b/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
index c662a04337b..c2e94efd9bd 100644
--- 
a/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
+++ 
b/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
@@ -22,14 +22,12 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
 import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
 import 
org.apache.shardingsphere.infra.instance.metadata.InstanceMetaDataBuilder;
-import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
 import 
org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlModeConfigurationSwapper;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.mode.manager.ContextManagerBuilder;
 import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
-import 
org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListener;
 import org.apache.shardingsphere.proxy.backend.config.ProxyConfiguration;
 import org.apache.shardingsphere.proxy.backend.config.YamlProxyConfiguration;
 import 
org.apache.shardingsphere.proxy.backend.config.yaml.swapper.YamlProxyConfigurationSwapper;
@@ -58,7 +56,6 @@ public final class BootstrapInitializer {
         ProxyConfiguration proxyConfig = new 
YamlProxyConfigurationSwapper().swap(yamlConfig);
         ContextManager contextManager = createContextManager(proxyConfig, 
modeConfig, port, force);
         ProxyContext.init(contextManager);
-        contextManagerInitializedCallback(contextManager);
         ShardingSphereProxyVersion.setVersion(contextManager);
     }
     
@@ -72,16 +69,4 @@ public final class BootstrapInitializer {
     private InstanceMetaData createInstanceMetaData(final int port) {
         return TypedSPILoader.getService(InstanceMetaDataBuilder.class, 
"Proxy").build(port);
     }
-    
-    private void contextManagerInitializedCallback(final ContextManager 
contextManager) {
-        for (ContextManagerLifecycleListener each : 
ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class))
 {
-            try {
-                each.onInitialized(contextManager);
-                // CHECKSTYLE:OFF
-            } catch (final RuntimeException ex) {
-                // CHECKSTYLE:ON
-                log.error("contextManager onInitialized callback for '{}' 
failed", each.getClass().getName(), ex);
-            }
-        }
-    }
 }

Reply via email to