Skip to content

Commit

Permalink
Merge pull request #300 from peachyy/dev
Browse files Browse the repository at this point in the history
issues 299 Unified management SPI
  • Loading branch information
yanhom1314 authored Jul 6, 2023
2 parents 9311be5 + 2a0a51f commit 4cdc5f3
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.dromara.dynamictp.common.util;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;

/**
* Unified ServiceLoader Help
*
* @author xs.Tao
* @since 1.1.4
*/

public class ExtensionServiceLoader {


private static final Map<Class<?>, List<?>> extensionListMap = new ConcurrentHashMap<>();

private ExtensionServiceLoader() {
}

/**
* loader service
* @param clazz SPI interface
* @return services
* @param <T> interface class
*/
@SuppressWarnings("unchecked")
public static <T> List<T> loader(Class<T> clazz) {
List<T> services = (List<T>) extensionListMap.get(clazz);
if (services == null) {
services = reLoaderList(clazz);
if (!services.isEmpty()) {
extensionListMap.put(clazz, services);
}
}
return services;
}

/**
* loader the first service
* @param clazz SPI interface
* @return service
* @param <T> interface class
*/
public static <T> T loaderFirst(Class<T> clazz){
List<T> services=loader(clazz);
return CollectionUtils.isEmpty(services)?null:services.get(0);
}

private static <T> List<T> reLoaderList(Class<T> clazz) {
ServiceLoader<T> serviceLoader = ServiceLoader.load(clazz);
List<T> services = new ArrayList<>();
for (T service : serviceLoader) {
services.add(service);
}
return services;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@

package org.dromara.dynamictp.core.handler;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.dromara.dynamictp.common.entity.ThreadPoolStats;
import org.dromara.dynamictp.common.util.ExtensionServiceLoader;
import org.dromara.dynamictp.core.monitor.collector.InternalLogCollector;
import org.dromara.dynamictp.core.monitor.collector.LogCollector;
import org.dromara.dynamictp.core.monitor.collector.MetricsCollector;
import org.dromara.dynamictp.core.monitor.collector.MicroMeterCollector;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;

/**
* CollectorHandler related
Expand All @@ -42,7 +42,7 @@ public final class CollectorHandler {
private static final Map<String, MetricsCollector> COLLECTORS = Maps.newHashMap();

private CollectorHandler() {
ServiceLoader<MetricsCollector> loader = ServiceLoader.load(MetricsCollector.class);
List<MetricsCollector> loader= ExtensionServiceLoader.loader(MetricsCollector.class);
for (MetricsCollector collector : loader) {
COLLECTORS.put(collector.type(), collector);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@

package org.dromara.dynamictp.core.handler;

import com.google.common.collect.Lists;
import org.dromara.dynamictp.common.em.ConfigFileTypeEnum;
import org.dromara.dynamictp.common.parser.config.ConfigParser;
import org.dromara.dynamictp.common.parser.config.JsonConfigParser;
import org.dromara.dynamictp.common.parser.config.PropertiesConfigParser;
import org.dromara.dynamictp.common.parser.config.YamlConfigParser;
import com.google.common.collect.Lists;
import org.dromara.dynamictp.common.util.ExtensionServiceLoader;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;

/**
* ConfigHandler related
Expand All @@ -41,7 +41,7 @@ public final class ConfigHandler {
private static final List<ConfigParser> PARSERS = Lists.newArrayList();

private ConfigHandler() {
ServiceLoader<ConfigParser> loader = ServiceLoader.load(ConfigParser.class);
List<ConfigParser> loader= ExtensionServiceLoader.loader(ConfigParser.class);
for (ConfigParser configParser : loader) {
PARSERS.add(configParser);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@

package org.dromara.dynamictp.core.handler;

import lombok.extern.slf4j.Slf4j;
import org.dromara.dynamictp.common.em.NotifyItemEnum;
import org.dromara.dynamictp.common.entity.NotifyItem;
import org.dromara.dynamictp.common.entity.TpMainFields;
import org.dromara.dynamictp.common.util.ExtensionServiceLoader;
import org.dromara.dynamictp.core.notifier.DtpDingNotifier;
import org.dromara.dynamictp.core.notifier.DtpLarkNotifier;
import org.dromara.dynamictp.core.notifier.DtpNotifier;
import org.dromara.dynamictp.core.notifier.DtpWechatNotifier;
import org.dromara.dynamictp.core.notifier.manager.NotifyHelper;
import org.dromara.dynamictp.core.notifier.base.DingNotifier;
import org.dromara.dynamictp.core.notifier.base.LarkNotifier;
import org.dromara.dynamictp.core.notifier.base.WechatNotifier;
import org.dromara.dynamictp.core.notifier.context.DtpNotifyCtxHolder;
import lombok.extern.slf4j.Slf4j;
import org.dromara.dynamictp.core.notifier.manager.NotifyHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;

/**
* NotifierHandler related
Expand All @@ -48,7 +48,7 @@ public final class NotifierHandler {
private static final Map<String, DtpNotifier> NOTIFIERS = new HashMap<>();

private NotifierHandler() {
ServiceLoader<DtpNotifier> loader = ServiceLoader.load(DtpNotifier.class);
List<DtpNotifier> loader= ExtensionServiceLoader.loader(DtpNotifier.class);
for (DtpNotifier notifier : loader) {
NOTIFIERS.put(notifier.platform(), notifier);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
package org.dromara.dynamictp.core.plugin;

import lombok.extern.slf4j.Slf4j;
import org.dromara.dynamictp.common.util.ExtensionServiceLoader;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ServiceLoader;

/**
* DtpInterceptorRegistry related
Expand All @@ -39,7 +39,7 @@ public class DtpInterceptorRegistry {
private static final List<DtpInterceptor> INTERCEPTORS = new ArrayList<>();

static {
ServiceLoader<DtpInterceptor> loader = ServiceLoader.load(DtpInterceptor.class);
List<DtpInterceptor> loader = ExtensionServiceLoader.loader(DtpInterceptor.class);
for (DtpInterceptor interceptor : loader) {
INTERCEPTORS.add(interceptor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@

import lombok.extern.slf4j.Slf4j;
import org.dromara.dynamictp.common.ex.DtpException;
import org.dromara.dynamictp.common.util.ExtensionServiceLoader;

import java.lang.reflect.Proxy;
import java.util.List;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

import static org.dromara.dynamictp.common.em.RejectedTypeEnum.ABORT_POLICY;
import static org.dromara.dynamictp.common.em.RejectedTypeEnum.CALLER_RUNS_POLICY;
import static org.dromara.dynamictp.common.em.RejectedTypeEnum.DISCARD_OLDEST_POLICY;
import static org.dromara.dynamictp.common.em.RejectedTypeEnum.DISCARD_POLICY;
import static org.dromara.dynamictp.common.em.RejectedTypeEnum.*;

/**
* RejectHandlerGetter related
Expand All @@ -52,8 +50,7 @@ public static RejectedExecutionHandler buildRejectedHandler(String name) {
} else if (Objects.equals(name, DISCARD_POLICY.getName())) {
return new ThreadPoolExecutor.DiscardPolicy();
}

ServiceLoader<RejectedExecutionHandler> serviceLoader = ServiceLoader.load(RejectedExecutionHandler.class);
List<RejectedExecutionHandler> serviceLoader= ExtensionServiceLoader.loader(RejectedExecutionHandler.class);
for (RejectedExecutionHandler handler : serviceLoader) {
String handlerName = handler.getClass().getSimpleName();
if (name.equalsIgnoreCase(handlerName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
import lombok.extern.slf4j.Slf4j;
import org.dromara.dynamictp.common.pattern.singleton.Singleton;
import org.dromara.dynamictp.common.properties.DtpProperties;
import org.dromara.dynamictp.common.util.ExtensionServiceLoader;
import org.dromara.dynamictp.core.spring.PropertiesBinder;
import org.springframework.core.env.Environment;

import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.ServiceLoader;

/**
* BinderHelper related
Expand All @@ -42,12 +41,12 @@ private static PropertiesBinder getBinder() {
if (Objects.nonNull(binder)) {
return binder;
}
final Iterator<PropertiesBinder> iterator = ServiceLoader.load(PropertiesBinder.class).iterator();
if (!iterator.hasNext()) {
final PropertiesBinder propertiesBinder = ExtensionServiceLoader.loaderFirst(PropertiesBinder.class);
if (null==propertiesBinder) {
log.error("DynamicTp refresh, no SPI for org.dromara.dynamictp.core.spring.PropertiesBinder.");
return null;
}
binder = iterator.next();
binder = propertiesBinder;
Singleton.INST.single(PropertiesBinder.class, binder);
return binder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

package org.dromara.dynamictp.core.support.task.wrapper;

import org.dromara.dynamictp.common.util.StringUtil;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.dromara.dynamictp.common.util.ExtensionServiceLoader;
import org.dromara.dynamictp.common.util.StringUtil;

import java.util.Collections;
import java.util.List;
import java.util.ServiceLoader;
import java.util.Set;

import static java.util.stream.Collectors.toList;
Expand All @@ -39,7 +39,7 @@ public class TaskWrappers {
private static final List<TaskWrapper> TASK_WRAPPERS = Lists.newArrayList();

private TaskWrappers() {
ServiceLoader<TaskWrapper> loader = ServiceLoader.load(TaskWrapper.class);
List<TaskWrapper> loader= ExtensionServiceLoader.loader(TaskWrapper.class);
for (TaskWrapper taskWrapper : loader) {
TASK_WRAPPERS.add(taskWrapper);
}
Expand Down
4 changes: 2 additions & 2 deletions example/example-nacos/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@
<dependency>
<groupId>org.dromara.dynamictp</groupId>
<artifactId>dynamic-tp-spring-boot-starter-nacos</artifactId>
<version>${revision}</version>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.dromara.dynamictp</groupId>
<artifactId>dynamic-tp-spring-boot-starter-adapter-webserver</artifactId>
<version>${revision}</version>
<version>1.1.3</version>
</dependency>

<!-- <dependency>-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.dromara.dynamictp.core.support.DynamicTp;
import org.dromara.dynamictp.core.support.ThreadPoolBuilder;
import org.dromara.dynamictp.core.support.ThreadPoolCreator;
import org.dromara.dynamictp.core.support.task.wrapper.TtlTaskWrapper;
import org.dromara.dynamictp.core.thread.DtpExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -91,6 +92,7 @@ public ThreadPoolExecutor dtpExecutor2() {
.workQueue(SYNCHRONOUS_QUEUE.getName(), null, false, null)
.waitForTasksToCompleteOnShutdown(true)
.awaitTerminationSeconds(5)
.taskWrapper(new TtlTaskWrapper())
.buildDynamic();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.dromara.dynamictp.test.common.util;

import org.dromara.dynamictp.common.util.ExtensionServiceLoader;
import org.dromara.dynamictp.core.plugin.DtpInterceptor;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;

import java.util.List;

/**
* ExtensionServiceLoader test case
*/
public class ExtensionServiceLoaderTest {

@Test
public void test1(){
List<DtpInterceptor> loader = ExtensionServiceLoader.loader(DtpInterceptor.class);
Assertions.assertTrue(loader.stream().anyMatch(it->it instanceof TestInterceptorLoader));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.dromara.dynamictp.test.common.util;

import org.dromara.dynamictp.core.plugin.DtpInterceptor;
import org.dromara.dynamictp.core.plugin.DtpInvocation;

public class TestInterceptorLoader implements DtpInterceptor {

@Override
public Object intercept(DtpInvocation invocation) throws Throwable {
return invocation.proceed();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.dromara.dynamictp.test.common.util.TestInterceptorLoader

0 comments on commit 4cdc5f3

Please sign in to comment.