diff --git a/ecosystem/executor/kernel/src/main/java/org/apache/shardingsphere/elasticjob/executor/item/JobItemExecutorFactory.java b/ecosystem/executor/kernel/src/main/java/org/apache/shardingsphere/elasticjob/executor/item/JobItemExecutorFactory.java index 9bf125e441..4632f8ce36 100644 --- a/ecosystem/executor/kernel/src/main/java/org/apache/shardingsphere/elasticjob/executor/item/JobItemExecutorFactory.java +++ b/ecosystem/executor/kernel/src/main/java/org/apache/shardingsphere/elasticjob/executor/item/JobItemExecutorFactory.java @@ -22,11 +22,7 @@ import org.apache.shardingsphere.elasticjob.api.ElasticJob; import org.apache.shardingsphere.elasticjob.executor.item.impl.ClassedJobItemExecutor; import org.apache.shardingsphere.elasticjob.infra.exception.JobConfigurationException; - -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.ServiceLoader; +import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; /** * Job item executor factory. @@ -35,12 +31,6 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class JobItemExecutorFactory { - private static final Map CLASSED_EXECUTORS = new HashMap<>(); - - static { - ServiceLoader.load(ClassedJobItemExecutor.class).forEach(each -> CLASSED_EXECUTORS.put(each.getElasticJobClass(), each)); - } - /** * Get executor. * @@ -49,9 +39,9 @@ public final class JobItemExecutorFactory { */ @SuppressWarnings("unchecked") public static JobItemExecutor getExecutor(final Class elasticJobClass) { - for (Entry entry : CLASSED_EXECUTORS.entrySet()) { - if (entry.getKey().isAssignableFrom(elasticJobClass)) { - return entry.getValue(); + for (ClassedJobItemExecutor each : ShardingSphereServiceLoader.getServiceInstances(ClassedJobItemExecutor.class)) { + if (each.getElasticJobClass().isAssignableFrom(elasticJobClass)) { + return each; } } throw new JobConfigurationException("Can not find executor for elastic job class `%s`", elasticJobClass.getName()); diff --git a/ecosystem/executor/kernel/src/main/java/org/apache/shardingsphere/elasticjob/executor/item/impl/ClassedJobItemExecutor.java b/ecosystem/executor/kernel/src/main/java/org/apache/shardingsphere/elasticjob/executor/item/impl/ClassedJobItemExecutor.java index 1627f6967d..63b8a06881 100644 --- a/ecosystem/executor/kernel/src/main/java/org/apache/shardingsphere/elasticjob/executor/item/impl/ClassedJobItemExecutor.java +++ b/ecosystem/executor/kernel/src/main/java/org/apache/shardingsphere/elasticjob/executor/item/impl/ClassedJobItemExecutor.java @@ -19,12 +19,14 @@ import org.apache.shardingsphere.elasticjob.api.ElasticJob; import org.apache.shardingsphere.elasticjob.executor.item.JobItemExecutor; +import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; /** * Classed job item executor. * * @param type of ElasticJob */ +@SingletonSPI public interface ClassedJobItemExecutor extends JobItemExecutor { /** diff --git a/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/JobTracingEventBus.java b/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/JobTracingEventBus.java index 8f39d39ca6..003e03837c 100644 --- a/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/JobTracingEventBus.java +++ b/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/JobTracingEventBus.java @@ -25,7 +25,8 @@ import org.apache.shardingsphere.elasticjob.tracing.api.TracingConfiguration; import org.apache.shardingsphere.elasticjob.tracing.event.JobEvent; import org.apache.shardingsphere.elasticjob.tracing.exception.TracingConfigurationException; -import org.apache.shardingsphere.elasticjob.tracing.listener.TracingListenerFactory; +import org.apache.shardingsphere.elasticjob.tracing.listener.TracingListenerConfiguration; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; @@ -64,9 +65,14 @@ private static ExecutorService createExecutorService(final int threadSize) { return MoreExecutors.listeningDecorator(MoreExecutors.getExitingExecutorService(threadPoolExecutor)); } + @SuppressWarnings("unchecked") private void register(final TracingConfiguration tracingConfig) { try { - eventBus.register(TracingListenerFactory.getListener(tracingConfig)); + if (null == tracingConfig.getTracingStorageConfiguration()) { + throw new TracingConfigurationException(String.format("Can not find executor service handler type '%s'.", tracingConfig.getType())); + } + eventBus.register( + TypedSPILoader.getService(TracingListenerConfiguration.class, tracingConfig.getType()).createTracingListener(tracingConfig.getTracingStorageConfiguration().getStorage())); isRegistered = true; } catch (final TracingConfigurationException ex) { log.error("Elastic job: create tracing listener failure, error is: ", ex); diff --git a/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/listener/TracingListenerConfiguration.java b/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/listener/TracingListenerConfiguration.java index 3fbb124e09..190dce213a 100644 --- a/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/listener/TracingListenerConfiguration.java +++ b/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/listener/TracingListenerConfiguration.java @@ -18,13 +18,16 @@ package org.apache.shardingsphere.elasticjob.tracing.listener; import org.apache.shardingsphere.elasticjob.tracing.exception.TracingConfigurationException; +import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI; /** * Tracing listener configuration. * * @param type of tracing storage */ -public interface TracingListenerConfiguration { +@SingletonSPI +public interface TracingListenerConfiguration extends TypedSPI { /** * Create tracing listener. @@ -35,10 +38,6 @@ public interface TracingListenerConfiguration { */ TracingListener createTracingListener(T storage) throws TracingConfigurationException; - /** - * Get tracing type. - * - * @return tracing type - */ + @Override String getType(); } diff --git a/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/listener/TracingListenerFactory.java b/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/listener/TracingListenerFactory.java deleted file mode 100644 index f4c451cee0..0000000000 --- a/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/listener/TracingListenerFactory.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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.elasticjob.tracing.listener; - -import com.google.common.base.Strings; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import org.apache.shardingsphere.elasticjob.tracing.api.TracingConfiguration; -import org.apache.shardingsphere.elasticjob.tracing.exception.TracingConfigurationException; - -import java.util.HashMap; -import java.util.Map; -import java.util.ServiceLoader; - -/** - * Tracing listener factory. - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class TracingListenerFactory { - - private static final Map LISTENER_CONFIGS = new HashMap<>(); - - static { - for (TracingListenerConfiguration each : ServiceLoader.load(TracingListenerConfiguration.class)) { - LISTENER_CONFIGS.put(each.getType(), each); - } - } - - /** - * Get tracing listener. - * - * @param tracingConfig tracing configuration - * @return tracing listener - * @throws TracingConfigurationException tracing configuration exception - */ - @SuppressWarnings("unchecked") - public static TracingListener getListener(final TracingConfiguration tracingConfig) throws TracingConfigurationException { - if (null == tracingConfig.getTracingStorageConfiguration() || Strings.isNullOrEmpty(tracingConfig.getType()) || !LISTENER_CONFIGS.containsKey(tracingConfig.getType())) { - throw new TracingConfigurationException(String.format("Can not find executor service handler type '%s'.", tracingConfig.getType())); - } - return LISTENER_CONFIGS.get(tracingConfig.getType()).createTracingListener(tracingConfig.getTracingStorageConfiguration().getStorage()); - } -} diff --git a/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/storage/TracingStorageConverter.java b/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/storage/TracingStorageConverter.java index d7686cbe72..559821e4e9 100644 --- a/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/storage/TracingStorageConverter.java +++ b/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/storage/TracingStorageConverter.java @@ -18,12 +18,14 @@ package org.apache.shardingsphere.elasticjob.tracing.storage; import org.apache.shardingsphere.elasticjob.tracing.api.TracingStorageConfiguration; +import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; /** * Tracing storage converter. * * @param storage type */ +@SingletonSPI public interface TracingStorageConverter { /** diff --git a/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/storage/TracingStorageConverterFactory.java b/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/storage/TracingStorageConverterFactory.java index 30c1004846..b326c1ee39 100644 --- a/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/storage/TracingStorageConverterFactory.java +++ b/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/storage/TracingStorageConverterFactory.java @@ -19,11 +19,9 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; -import java.util.LinkedList; -import java.util.List; import java.util.Optional; -import java.util.ServiceLoader; /** * Factory for {@link TracingStorageConverter}. @@ -31,21 +29,16 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class TracingStorageConverterFactory { - private static final List> CONVERTERS = new LinkedList<>(); - - static { - ServiceLoader.load(TracingStorageConverter.class).forEach(CONVERTERS::add); - } - /** * Find {@link TracingStorageConverter} for specific storage type. * * @param storageType storage type - * @param storage type + * @param storage type * @return instance of {@link TracingStorageConverter} */ @SuppressWarnings("unchecked") public static Optional> findConverter(final Class storageType) { - return CONVERTERS.stream().filter(each -> each.storageType().isAssignableFrom(storageType)).map(each -> (TracingStorageConverter) each).findFirst(); + return ShardingSphereServiceLoader.getServiceInstances(TracingStorageConverter.class).stream() + .filter(each -> each.storageType().isAssignableFrom(storageType)).map(each -> (TracingStorageConverter) each).findFirst(); } } diff --git a/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/yaml/YamlTracingConfigurationConverter.java b/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/yaml/YamlTracingConfigurationConverter.java index 5d7bf346ae..d605fcce51 100644 --- a/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/yaml/YamlTracingConfigurationConverter.java +++ b/ecosystem/tracing/api/src/main/java/org/apache/shardingsphere/elasticjob/tracing/yaml/YamlTracingConfigurationConverter.java @@ -18,10 +18,9 @@ package org.apache.shardingsphere.elasticjob.tracing.yaml; import org.apache.shardingsphere.elasticjob.infra.yaml.config.YamlConfigurationConverter; -import org.apache.shardingsphere.elasticjob.infra.yaml.config.YamlConfigurationConverterFactory; -import org.apache.shardingsphere.elasticjob.infra.yaml.exception.YamlConfigurationConverterNotFoundException; import org.apache.shardingsphere.elasticjob.tracing.api.TracingConfiguration; import org.apache.shardingsphere.elasticjob.tracing.api.TracingStorageConfiguration; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; /** * Converter to convert {@link TracingConfiguration} to {@link YamlTracingConfiguration}. @@ -32,21 +31,19 @@ public final class YamlTracingConfigurationConverter implements YamlConfigurationConverter, YamlTracingConfiguration> { @Override - public YamlTracingConfiguration convertToYamlConfiguration(final TracingConfiguration tracingConfiguration) { + public YamlTracingConfiguration convertToYamlConfiguration(final TracingConfiguration tracingConfig) { YamlTracingConfiguration result = new YamlTracingConfiguration<>(); - result.setType(tracingConfiguration.getType()); - result.setTracingStorageConfiguration(convertTracingStorageConfiguration(tracingConfiguration.getTracingStorageConfiguration())); + result.setType(tracingConfig.getType()); + result.setTracingStorageConfiguration(convertTracingStorageConfiguration(tracingConfig.getTracingStorageConfiguration())); return result; } - private YamlTracingStorageConfiguration convertTracingStorageConfiguration(final TracingStorageConfiguration tracingStorageConfiguration) { - return YamlConfigurationConverterFactory - ., YamlTracingStorageConfiguration>findConverter((Class>) tracingStorageConfiguration.getClass()) - .orElseThrow(() -> new YamlConfigurationConverterNotFoundException(tracingStorageConfiguration.getClass())).convertToYamlConfiguration(tracingStorageConfiguration); + private YamlTracingStorageConfiguration convertTracingStorageConfiguration(final TracingStorageConfiguration tracingStorageConfig) { + return (YamlTracingStorageConfiguration) TypedSPILoader.getService(YamlConfigurationConverter.class, tracingStorageConfig.getClass()).convertToYamlConfiguration(tracingStorageConfig); } @Override - public Class configurationType() { + public Class getType() { return TracingConfiguration.class; } } diff --git a/ecosystem/tracing/api/src/test/java/org/apache/shardingsphere/elasticjob/tracing/listener/TracingListenerFactoryTest.java b/ecosystem/tracing/api/src/test/java/org/apache/shardingsphere/elasticjob/tracing/listener/TracingListenerFactoryTest.java deleted file mode 100644 index f6ab431e9c..0000000000 --- a/ecosystem/tracing/api/src/test/java/org/apache/shardingsphere/elasticjob/tracing/listener/TracingListenerFactoryTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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.elasticjob.tracing.listener; - -import org.apache.shardingsphere.elasticjob.tracing.api.TracingConfiguration; -import org.apache.shardingsphere.elasticjob.tracing.exception.TracingConfigurationException; -import org.apache.shardingsphere.elasticjob.tracing.fixture.JobEventCallerConfiguration; -import org.apache.shardingsphere.elasticjob.tracing.fixture.TestTracingListener; -import org.junit.jupiter.api.Test; - -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; - -class TracingListenerFactoryTest { - - @Test - void assertGetListenerWithNullType() { - assertThrows(TracingConfigurationException.class, () -> TracingListenerFactory.getListener(new TracingConfiguration<>("", null))); - } - - @Test - void assertGetInvalidListener() { - assertThrows(TracingConfigurationException.class, () -> TracingListenerFactory.getListener(new TracingConfiguration<>("INVALID", null))); - } - - @Test - void assertGetListener() throws TracingConfigurationException { - assertThat(TracingListenerFactory.getListener(new TracingConfiguration<>("TEST", new JobEventCallerConfiguration(() -> { - }))), instanceOf(TestTracingListener.class)); - } -} diff --git a/ecosystem/tracing/api/src/test/java/org/apache/shardingsphere/elasticjob/tracing/yaml/YamlJobEventCallerConfigurationConverter.java b/ecosystem/tracing/api/src/test/java/org/apache/shardingsphere/elasticjob/tracing/yaml/YamlJobEventCallerConfigurationConverter.java index 22d282c183..b25b87ebfa 100644 --- a/ecosystem/tracing/api/src/test/java/org/apache/shardingsphere/elasticjob/tracing/yaml/YamlJobEventCallerConfigurationConverter.java +++ b/ecosystem/tracing/api/src/test/java/org/apache/shardingsphere/elasticjob/tracing/yaml/YamlJobEventCallerConfigurationConverter.java @@ -36,7 +36,7 @@ public YamlTracingStorageConfiguration convertToYamlConfiguratio } @Override - public Class configurationType() { + public Class getType() { return JobEventCallerConfiguration.class; } } diff --git a/ecosystem/tracing/rdb/src/main/java/org/apache/shardingsphere/elasticjob/tracing/rdb/datasource/DataSourceConfiguration.java b/ecosystem/tracing/rdb/src/main/java/org/apache/shardingsphere/elasticjob/tracing/rdb/datasource/DataSourceConfiguration.java index 3b9bc86187..63434bf611 100644 --- a/ecosystem/tracing/rdb/src/main/java/org/apache/shardingsphere/elasticjob/tracing/rdb/datasource/DataSourceConfiguration.java +++ b/ecosystem/tracing/rdb/src/main/java/org/apache/shardingsphere/elasticjob/tracing/rdb/datasource/DataSourceConfiguration.java @@ -25,6 +25,7 @@ import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import org.apache.shardingsphere.elasticjob.tracing.api.TracingStorageConfiguration; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import javax.sql.DataSource; import java.lang.reflect.Method; @@ -35,7 +36,6 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Optional; -import java.util.ServiceLoader; /** * Data source configuration. @@ -121,20 +121,10 @@ public DataSource createDataSource() { setterMethod.get().invoke(result, entry.getValue()); } } - Optional decorator = findJDBCParameterDecorator(result); + Optional decorator = TypedSPILoader.findService(JDBCParameterDecorator.class, result.getClass()); return decorator.isPresent() ? decorator.get().decorate(result) : result; } - @SuppressWarnings("rawtypes") - private Optional findJDBCParameterDecorator(final DataSource dataSource) { - for (JDBCParameterDecorator each : ServiceLoader.load(JDBCParameterDecorator.class)) { - if (each.getType() == dataSource.getClass()) { - return Optional.of(each); - } - } - return Optional.empty(); - } - private Optional findSetterMethod(final Method[] methods, final String property) { String setterMethodName = Joiner.on("").join(SETTER_PREFIX, CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, property)); for (Method each : methods) { diff --git a/ecosystem/tracing/rdb/src/main/java/org/apache/shardingsphere/elasticjob/tracing/rdb/datasource/JDBCParameterDecorator.java b/ecosystem/tracing/rdb/src/main/java/org/apache/shardingsphere/elasticjob/tracing/rdb/datasource/JDBCParameterDecorator.java index 95fad6118e..e924b01eff 100644 --- a/ecosystem/tracing/rdb/src/main/java/org/apache/shardingsphere/elasticjob/tracing/rdb/datasource/JDBCParameterDecorator.java +++ b/ecosystem/tracing/rdb/src/main/java/org/apache/shardingsphere/elasticjob/tracing/rdb/datasource/JDBCParameterDecorator.java @@ -17,6 +17,8 @@ package org.apache.shardingsphere.elasticjob.tracing.rdb.datasource; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI; + import javax.sql.DataSource; /** @@ -24,7 +26,7 @@ * * @param type of data source */ -public interface JDBCParameterDecorator { +public interface JDBCParameterDecorator extends TypedSPI { /** * Decorate data source. @@ -34,10 +36,6 @@ public interface JDBCParameterDecorator { */ T decorate(T dataSource); - /** - * Get data source type. - * - * @return data source type - */ + @Override Class getType(); } diff --git a/ecosystem/tracing/rdb/src/main/java/org/apache/shardingsphere/elasticjob/tracing/rdb/storage/RDBJobEventStorage.java b/ecosystem/tracing/rdb/src/main/java/org/apache/shardingsphere/elasticjob/tracing/rdb/storage/RDBJobEventStorage.java index 18f05eb349..9680901d38 100644 --- a/ecosystem/tracing/rdb/src/main/java/org/apache/shardingsphere/elasticjob/tracing/rdb/storage/RDBJobEventStorage.java +++ b/ecosystem/tracing/rdb/src/main/java/org/apache/shardingsphere/elasticjob/tracing/rdb/storage/RDBJobEventStorage.java @@ -25,6 +25,7 @@ import org.apache.shardingsphere.elasticjob.tracing.exception.WrapException; import org.apache.shardingsphere.elasticjob.tracing.rdb.type.DatabaseType; import org.apache.shardingsphere.elasticjob.tracing.rdb.type.impl.DefaultDatabaseType; +import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; import javax.sql.DataSource; import java.sql.Connection; @@ -36,10 +37,8 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.ServiceLoader; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Supplier; @@ -56,8 +55,6 @@ public final class RDBJobEventStorage { private static final String TASK_ID_STATE_INDEX = "TASK_ID_STATE_INDEX"; - private static final Map DATABASE_TYPES = new HashMap<>(); - private static final Map STORAGE_MAP = new ConcurrentHashMap<>(); private final DataSource dataSource; @@ -66,12 +63,6 @@ public final class RDBJobEventStorage { private final RDBStorageSQLMapper sqlMapper; - static { - for (DatabaseType each : ServiceLoader.load(DatabaseType.class)) { - DATABASE_TYPES.put(each.getType(), each); - } - } - private RDBJobEventStorage(final DataSource dataSource) throws SQLException { this.dataSource = dataSource; databaseType = getDatabaseType(dataSource); @@ -117,7 +108,7 @@ public static RDBJobEventStorage wrapException(final Supplier convertToYamlConfiguration(fi } @Override - public Class configurationType() { + public Class getType() { return DataSourceConfiguration.class; } } diff --git a/infra/src/main/java/org/apache/shardingsphere/elasticjob/infra/pojo/JobConfigurationPOJO.java b/infra/src/main/java/org/apache/shardingsphere/elasticjob/infra/pojo/JobConfigurationPOJO.java index 3cdfd35d49..4d3fa13c96 100644 --- a/infra/src/main/java/org/apache/shardingsphere/elasticjob/infra/pojo/JobConfigurationPOJO.java +++ b/infra/src/main/java/org/apache/shardingsphere/elasticjob/infra/pojo/JobConfigurationPOJO.java @@ -22,8 +22,8 @@ import org.apache.shardingsphere.elasticjob.api.JobConfiguration; import org.apache.shardingsphere.elasticjob.api.JobExtraConfiguration; import org.apache.shardingsphere.elasticjob.infra.yaml.config.YamlConfiguration; -import org.apache.shardingsphere.elasticjob.infra.yaml.config.YamlConfigurationConverterFactory; -import org.apache.shardingsphere.elasticjob.infra.yaml.exception.YamlConfigurationConverterNotFoundException; +import org.apache.shardingsphere.elasticjob.infra.yaml.config.YamlConfigurationConverter; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import java.util.ArrayList; import java.util.Collection; @@ -125,8 +125,8 @@ public static JobConfigurationPOJO fromJobConfiguration(final JobConfiguration j result.setJobExecutorServiceHandlerType(jobConfiguration.getJobExecutorServiceHandlerType()); result.setJobErrorHandlerType(jobConfiguration.getJobErrorHandlerType()); result.setJobListenerTypes(jobConfiguration.getJobListenerTypes()); - jobConfiguration.getExtraConfigurations().stream().map(each -> YamlConfigurationConverterFactory.findConverter((Class) each.getClass()) - .orElseThrow(() -> new YamlConfigurationConverterNotFoundException(each.getClass())).convertToYamlConfiguration(each)).forEach(result.getJobExtraConfigurations()::add); + jobConfiguration.getExtraConfigurations().stream() + .map(each -> TypedSPILoader.getService(YamlConfigurationConverter.class, each.getClass()).convertToYamlConfiguration(each)).forEach(result.getJobExtraConfigurations()::add); result.setDescription(jobConfiguration.getDescription()); result.setProps(jobConfiguration.getProps()); result.setDisabled(jobConfiguration.isDisabled()); diff --git a/infra/src/main/java/org/apache/shardingsphere/elasticjob/infra/yaml/config/YamlConfigurationConverter.java b/infra/src/main/java/org/apache/shardingsphere/elasticjob/infra/yaml/config/YamlConfigurationConverter.java index c15f208464..29ad03bb71 100644 --- a/infra/src/main/java/org/apache/shardingsphere/elasticjob/infra/yaml/config/YamlConfigurationConverter.java +++ b/infra/src/main/java/org/apache/shardingsphere/elasticjob/infra/yaml/config/YamlConfigurationConverter.java @@ -17,13 +17,17 @@ package org.apache.shardingsphere.elasticjob.infra.yaml.config; +import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI; + /** * YAML configuration converter. * * @param type of original configuration object * @param type of YAML configuration */ -public interface YamlConfigurationConverter> { +@SingletonSPI +public interface YamlConfigurationConverter> extends TypedSPI { /** * Convert to YAML configuration. @@ -33,10 +37,6 @@ public interface YamlConfigurationConverter> { */ Y convertToYamlConfiguration(T data); - /** - * Get type of Configuration. - * - * @return configuration type - */ - Class configurationType(); + @Override + Class getType(); } diff --git a/infra/src/main/java/org/apache/shardingsphere/elasticjob/infra/yaml/config/YamlConfigurationConverterFactory.java b/infra/src/main/java/org/apache/shardingsphere/elasticjob/infra/yaml/config/YamlConfigurationConverterFactory.java deleted file mode 100644 index 7303894ceb..0000000000 --- a/infra/src/main/java/org/apache/shardingsphere/elasticjob/infra/yaml/config/YamlConfigurationConverterFactory.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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.elasticjob.infra.yaml.config; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Optional; -import java.util.ServiceLoader; - -/** - * Factory for {@link YamlConfigurationConverter}. - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class YamlConfigurationConverterFactory { - - private static final Map, YamlConfigurationConverter> CONVERTERS = new LinkedHashMap<>(); - - static { - ServiceLoader.load(YamlConfigurationConverter.class).forEach(each -> CONVERTERS.put(each.configurationType(), each)); - } - - /** - * Find {@link YamlConfigurationConverter} for specific configuration type. - * - * @param configurationType type of configuration - * @param type of configuration - * @param type of YAML configuration - * @return converter for specific configuration type - */ - @SuppressWarnings("unchecked") - public static > Optional> findConverter(final Class configurationType) { - return Optional.ofNullable((YamlConfigurationConverter) CONVERTERS.get(configurationType)); - } -} diff --git a/infra/src/test/java/org/apache/shardingsphere/elasticjob/infra/yaml/config/YamlConfigurationConverterFactoryTest.java b/infra/src/test/java/org/apache/shardingsphere/elasticjob/infra/yaml/config/YamlConfigurationConverterFactoryTest.java deleted file mode 100644 index 7d2a2f0946..0000000000 --- a/infra/src/test/java/org/apache/shardingsphere/elasticjob/infra/yaml/config/YamlConfigurationConverterFactoryTest.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.elasticjob.infra.yaml.config; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertFalse; - -class YamlConfigurationConverterFactoryTest { - - @Test - void assertConverterNotFound() { - assertFalse(YamlConfigurationConverterFactory.findConverter(AClassWithoutCorrespondingConverter.class).isPresent()); - } - - private static class AClassWithoutCorrespondingConverter { - } -} diff --git a/kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/setup/JobClassNameProvider.java b/kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/setup/JobClassNameProvider.java index 36730e4aa6..99bc7a7194 100644 --- a/kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/setup/JobClassNameProvider.java +++ b/kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/setup/JobClassNameProvider.java @@ -18,10 +18,12 @@ package org.apache.shardingsphere.elasticjob.kernel.internal.setup; import org.apache.shardingsphere.elasticjob.api.ElasticJob; +import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; /** * Job class name provider. */ +@SingletonSPI public interface JobClassNameProvider { /** diff --git a/kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/setup/JobClassNameProviderFactory.java b/kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/setup/JobClassNameProviderFactory.java index d2d7cd634d..914b1ecea1 100644 --- a/kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/setup/JobClassNameProviderFactory.java +++ b/kernel/src/main/java/org/apache/shardingsphere/elasticjob/kernel/internal/setup/JobClassNameProviderFactory.java @@ -19,10 +19,9 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; -import java.util.LinkedList; -import java.util.List; -import java.util.ServiceLoader; +import java.util.Collection; /** * Job class name provider factory. @@ -30,22 +29,15 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class JobClassNameProviderFactory { - private static final List PROVIDERS = new LinkedList<>(); - private static final JobClassNameProvider DEFAULT_PROVIDER = new DefaultJobClassNameProvider(); - static { - for (JobClassNameProvider each : ServiceLoader.load(JobClassNameProvider.class)) { - PROVIDERS.add(each); - } - } - /** * Get the first job class name provider. * * @return job class name provider */ public static JobClassNameProvider getProvider() { - return PROVIDERS.isEmpty() ? DEFAULT_PROVIDER : PROVIDERS.get(0); + Collection jobClassNameProviders = ShardingSphereServiceLoader.getServiceInstances(JobClassNameProvider.class); + return jobClassNameProviders.isEmpty() ? DEFAULT_PROVIDER : jobClassNameProviders.iterator().next(); } } diff --git a/registry-center/api/pom.xml b/registry-center/api/pom.xml index bfb3dab209..a5a5ef9d77 100644 --- a/registry-center/api/pom.xml +++ b/registry-center/api/pom.xml @@ -25,4 +25,11 @@ elasticjob-registry-center-api ${project.artifactId} + + + + org.apache.shardingsphere + shardingsphere-infra-spi + + diff --git a/registry-center/api/src/main/java/org/apache/shardingsphere/elasticjob/reg/exception/IgnoredExceptionProvider.java b/registry-center/api/src/main/java/org/apache/shardingsphere/elasticjob/reg/exception/IgnoredExceptionProvider.java index 1f52a6b07b..21e683afc2 100644 --- a/registry-center/api/src/main/java/org/apache/shardingsphere/elasticjob/reg/exception/IgnoredExceptionProvider.java +++ b/registry-center/api/src/main/java/org/apache/shardingsphere/elasticjob/reg/exception/IgnoredExceptionProvider.java @@ -17,11 +17,14 @@ package org.apache.shardingsphere.elasticjob.reg.exception; +import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; + import java.util.Collection; /** * Ignored exception provider. */ +@SingletonSPI public interface IgnoredExceptionProvider { /** diff --git a/registry-center/api/src/main/java/org/apache/shardingsphere/elasticjob/reg/exception/RegExceptionHandler.java b/registry-center/api/src/main/java/org/apache/shardingsphere/elasticjob/reg/exception/RegExceptionHandler.java index 2100a0fd49..42f59f8f67 100644 --- a/registry-center/api/src/main/java/org/apache/shardingsphere/elasticjob/reg/exception/RegExceptionHandler.java +++ b/registry-center/api/src/main/java/org/apache/shardingsphere/elasticjob/reg/exception/RegExceptionHandler.java @@ -20,24 +20,15 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; - -import java.util.Collection; -import java.util.LinkedList; -import java.util.ServiceLoader; +import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; /** * Registry center exception handler. */ -@Slf4j @NoArgsConstructor(access = AccessLevel.PRIVATE) +@Slf4j public final class RegExceptionHandler { - private static final Collection> IGNORED_EXCEPTIONS = new LinkedList<>(); - - static { - ServiceLoader.load(IgnoredExceptionProvider.class).forEach(each -> IGNORED_EXCEPTIONS.addAll(each.getIgnoredExceptions())); - } - /** * Handle exception. * @@ -57,6 +48,6 @@ public static void handleException(final Exception cause) { } private static boolean isIgnoredException(final Throwable cause) { - return IGNORED_EXCEPTIONS.stream().anyMatch(each -> each.isInstance(cause)); + return ShardingSphereServiceLoader.getServiceInstances(IgnoredExceptionProvider.class).stream().flatMap(each -> each.getIgnoredExceptions().stream()).anyMatch(each -> each.isInstance(cause)); } } diff --git a/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerFactory.java b/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerFactory.java index 5b5df09b7f..015ae524e9 100644 --- a/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerFactory.java +++ b/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerFactory.java @@ -20,10 +20,10 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.shardingsphere.elasticjob.restful.deserializer.factory.DeserializerFactory; +import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; import java.util.Map; import java.util.Optional; -import java.util.ServiceLoader; import java.util.concurrent.ConcurrentHashMap; /** @@ -50,10 +50,10 @@ public T deserialize(final Class targetType, final byte[] requestBodyByte }; static { - for (RequestBodyDeserializer deserializer : ServiceLoader.load(RequestBodyDeserializer.class)) { + for (RequestBodyDeserializer deserializer : ShardingSphereServiceLoader.getServiceInstances(RequestBodyDeserializer.class)) { REQUEST_BODY_DESERIALIZERS.put(deserializer.mimeType(), deserializer); } - for (DeserializerFactory factory : ServiceLoader.load(DeserializerFactory.class)) { + for (DeserializerFactory factory : ShardingSphereServiceLoader.getServiceInstances(DeserializerFactory.class)) { DEFAULT_REQUEST_BODY_DESERIALIZER_FACTORIES.put(factory.mimeType(), factory); } } diff --git a/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/factory/DeserializerFactory.java b/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/factory/DeserializerFactory.java index 5106d85dd0..db61b536ae 100644 --- a/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/factory/DeserializerFactory.java +++ b/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/factory/DeserializerFactory.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.elasticjob.restful.deserializer.factory; import org.apache.shardingsphere.elasticjob.restful.deserializer.RequestBodyDeserializer; +import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; /** * Deserializer factory. @@ -25,6 +26,7 @@ * @see RequestBodyDeserializer * @see org.apache.shardingsphere.elasticjob.restful.deserializer.RequestBodyDeserializerFactory */ +@SingletonSPI public interface DeserializerFactory { /** diff --git a/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/impl/DefaultTextPlainRequestBodyDeserializer.java b/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/impl/DefaultTextPlainRequestBodyDeserializer.java index d27c76197d..ccaa133a62 100644 --- a/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/impl/DefaultTextPlainRequestBodyDeserializer.java +++ b/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/impl/DefaultTextPlainRequestBodyDeserializer.java @@ -19,6 +19,7 @@ import io.netty.handler.codec.http.HttpHeaderValues; import org.apache.shardingsphere.elasticjob.restful.deserializer.RequestBodyDeserializer; +import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; import java.nio.charset.StandardCharsets; import java.text.MessageFormat; @@ -26,6 +27,7 @@ /** * Default deserializer for text/plain. */ +@SingletonSPI public final class DefaultTextPlainRequestBodyDeserializer implements RequestBodyDeserializer { @Override diff --git a/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializer.java b/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializer.java index 2aefd3cb16..4ce24a6000 100644 --- a/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializer.java +++ b/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializer.java @@ -17,9 +17,12 @@ package org.apache.shardingsphere.elasticjob.restful.serializer; +import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; + /** * Serializer for serializing response body with specific MIME type. */ +@SingletonSPI public interface ResponseBodySerializer { /** diff --git a/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerFactory.java b/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerFactory.java index 33e221912c..909652bf8d 100644 --- a/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerFactory.java +++ b/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerFactory.java @@ -20,10 +20,10 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.shardingsphere.elasticjob.restful.serializer.factory.SerializerFactory; +import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; import java.util.Map; import java.util.Optional; -import java.util.ServiceLoader; import java.util.concurrent.ConcurrentHashMap; /** @@ -50,10 +50,10 @@ public byte[] serialize(final Object responseBody) { }; static { - for (ResponseBodySerializer serializer : ServiceLoader.load(ResponseBodySerializer.class)) { + for (ResponseBodySerializer serializer : ShardingSphereServiceLoader.getServiceInstances(ResponseBodySerializer.class)) { RESPONSE_BODY_SERIALIZERS.put(serializer.mimeType(), serializer); } - for (SerializerFactory factory : ServiceLoader.load(SerializerFactory.class)) { + for (SerializerFactory factory : ShardingSphereServiceLoader.getServiceInstances(SerializerFactory.class)) { RESPONSE_BODY_SERIALIZER_FACTORIES.put(factory.mimeType(), factory); } } diff --git a/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/factory/SerializerFactory.java b/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/factory/SerializerFactory.java index 4f10d92f24..ee1fc329fc 100644 --- a/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/factory/SerializerFactory.java +++ b/restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/factory/SerializerFactory.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.elasticjob.restful.serializer.factory; import org.apache.shardingsphere.elasticjob.restful.serializer.ResponseBodySerializer; +import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; /** * Serializer factory. @@ -25,6 +26,7 @@ * @see ResponseBodySerializer * @see org.apache.shardingsphere.elasticjob.restful.serializer.ResponseBodySerializerFactory */ +@SingletonSPI public interface SerializerFactory { /**