You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/04/08 13:32:44 UTC
[shardingsphere] branch master updated: Refactor ContextManagerLifecycleListener (#16672)
This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 5b238f53500 Refactor ContextManagerLifecycleListener (#16672)
5b238f53500 is described below
commit 5b238f535009be4025c5a019bd8b5f32b74c0276
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri Apr 8 21:32:37 2022 +0800
Refactor ContextManagerLifecycleListener (#16672)
* Refactor ContextManagerLifecycleListener
* Refactor ContextManagerLifecycleListener
---
.../query/ShowScalingCheckAlgorithmsQueryResultSet.java | 12 ++++++------
.../pipeline/core/api/impl/RuleAlteredJobAPIImpl.java | 12 ++++++------
.../manager/listener/ContextManagerLifecycleListener.java | 5 ++---
.../proxy/initializer/BootstrapInitializer.java | 4 ++--
.../shardingsphere/spi/type/typed/TypedSPIRegistry.java | 15 +++++++++++++++
5 files changed, 31 insertions(+), 17 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/scaling/distsql/handler/query/ShowScalingCheckAlgorithmsQueryResultSet.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/scaling/distsql/handler/query/ShowScalingCheckAlgorithmsQueryResultSet.java
index a81fd5c0f13..df311a9a668 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/scaling/distsql/handler/query/ShowScalingCheckAlgorithmsQueryResultSet.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/scaling/distsql/handler/query/ShowScalingCheckAlgorithmsQueryResultSet.java
@@ -44,12 +44,12 @@ public final class ShowScalingCheckAlgorithmsQueryResultSet implements DistSQLRe
public void init(final ShardingSphereMetaData metaData, final SQLStatement sqlStatement) {
data = RULE_ALTERED_JOB_API.listDataConsistencyCheckAlgorithms().stream()
.map(each -> {
- Collection<Object> list = new LinkedList<>();
- list.add(each.getType());
- list.add(each.getDescription());
- list.add(Joiner.on(",").join(each.getSupportedDatabaseTypes()));
- list.add(each.getProvider());
- return list;
+ Collection<Object> result = new LinkedList<>();
+ result.add(each.getType());
+ result.add(each.getDescription());
+ result.add(Joiner.on(",").join(each.getSupportedDatabaseTypes()));
+ result.add(each.getProvider());
+ return result;
}).collect(Collectors.toList()).iterator();
}
diff --git a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/api/impl/RuleAlteredJobAPIImpl.java b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/api/impl/RuleAlteredJobAPIImpl.java
index 426a1fc6cc1..9af3bc86893 100644
--- a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/api/impl/RuleAlteredJobAPIImpl.java
+++ b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/api/impl/RuleAlteredJobAPIImpl.java
@@ -245,12 +245,12 @@ public final class RuleAlteredJobAPIImpl extends AbstractPipelineJobAPIImpl impl
checkModeConfig();
return DATA_CONSISTENCY_CHECK_ALGORITHM_MAP.values()
.stream().map(each -> {
- DataConsistencyCheckAlgorithmInfo algorithmInfo = new DataConsistencyCheckAlgorithmInfo();
- algorithmInfo.setType(each.getType());
- algorithmInfo.setDescription(each.getDescription());
- algorithmInfo.setSupportedDatabaseTypes(each.getSupportedDatabaseTypes());
- algorithmInfo.setProvider(each.getProvider());
- return algorithmInfo;
+ DataConsistencyCheckAlgorithmInfo result = new DataConsistencyCheckAlgorithmInfo();
+ result.setType(each.getType());
+ result.setDescription(each.getDescription());
+ result.setSupportedDatabaseTypes(each.getSupportedDatabaseTypes());
+ result.setProvider(each.getProvider());
+ return result;
}).collect(Collectors.toList());
}
diff --git a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/listener/ContextManagerLifecycleListener.java b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/listener/ContextManagerLifecycleListener.java
index ad6bc4c0c72..0f3913a2a8d 100644
--- a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/listener/ContextManagerLifecycleListener.java
+++ b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/listener/ContextManagerLifecycleListener.java
@@ -19,8 +19,7 @@ package org.apache.shardingsphere.mode.manager.listener;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.spi.type.singleton.SingletonSPI;
-import org.apache.shardingsphere.spi.type.typed.TypedSPI;
+import org.apache.shardingsphere.spi.type.typed.StatelessTypedSPI;
/**
* Context manager lifecycle listener.
@@ -28,7 +27,7 @@ import org.apache.shardingsphere.spi.type.typed.TypedSPI;
* It just support <code>proxy</code> mode for now, <code>JDBC</code> mode is not supported.
* </p>
*/
-public interface ContextManagerLifecycleListener extends TypedSPI, SingletonSPI {
+public interface ContextManagerLifecycleListener extends StatelessTypedSPI {
/**
* Callback on initialized.
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
index d1953df9af3..9eb87ce6448 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
@@ -33,7 +33,7 @@ import org.apache.shardingsphere.proxy.backend.config.yaml.swapper.YamlProxyConf
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.version.ShardingSphereProxyVersion;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.spi.type.singleton.SingletonSPIRegistry;
+import org.apache.shardingsphere.spi.type.typed.TypedSPIRegistry;
import java.sql.SQLException;
import java.util.Map;
@@ -78,7 +78,7 @@ public final class BootstrapInitializer {
}
private void contextManagerInitializedCallback(final ModeConfiguration modeConfig, final ContextManager contextManager) {
- Map<String, ContextManagerLifecycleListener> listeners = SingletonSPIRegistry.getTypedSingletonInstancesMap(ContextManagerLifecycleListener.class);
+ Map<String, ContextManagerLifecycleListener> listeners = TypedSPIRegistry.getRegisteredServices(ContextManagerLifecycleListener.class);
log.info("listeners.keySet={}", listeners.keySet());
for (Entry<String, ContextManagerLifecycleListener> entry : listeners.entrySet()) {
try {
diff --git a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/type/typed/TypedSPIRegistry.java b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/type/typed/TypedSPIRegistry.java
index 5616c730388..c83aeee8593 100644
--- a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/type/typed/TypedSPIRegistry.java
+++ b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/type/typed/TypedSPIRegistry.java
@@ -22,8 +22,11 @@ import lombok.NoArgsConstructor;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.spi.exception.ServiceProviderNotFoundException;
+import java.util.Map;
import java.util.Optional;
import java.util.Properties;
+import java.util.function.Function;
+import java.util.stream.Collectors;
/**
* Typed SPI registry.
@@ -112,4 +115,16 @@ public final class TypedSPIRegistry {
}
throw new ServiceProviderNotFoundException(spiClass, type);
}
+
+ /**
+ * Get registered service.
+ *
+ * @param spiClass stateless typed SPI class
+ * @param <T> SPI class type
+ * @return registered service
+ */
+ public static <T extends StatelessTypedSPI> Map<String, T> getRegisteredServices(final Class<T> spiClass) {
+ return ShardingSphereServiceLoader.getSingletonServiceInstances(spiClass).stream().collect(Collectors.toMap(TypedSPI::getType, Function.identity()));
+ }
}
+
\ No newline at end of file