소스 검색

Add loadInstanceList support for SpiLoader

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 5 년 전
부모
커밋
c316211faf
1개의 변경된 파일31개의 추가작업 그리고 1개의 파일을 삭제
  1. +31
    -1
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java

+ 31
- 1
sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java 파일 보기

@@ -92,7 +92,37 @@ public final class SpiLoader {
}

/**
* Load and sorted SPI instance list.
* Load the SPI instance list for provided SPI interface.
*
* @param clazz class of the SPI
* @param <T> SPI type
* @return sorted SPI instance list
* @since 1.6.0
*/
public static <T> List<T> loadInstanceList(Class<T> clazz) {
try {
String key = clazz.getName();
// Not thread-safe, as it's expected to be resolved in a thread-safe context.
ServiceLoader<T> serviceLoader = SERVICE_LOADER_MAP.get(key);
if (serviceLoader == null) {
serviceLoader = ServiceLoader.load(clazz);
SERVICE_LOADER_MAP.put(key, serviceLoader);
}

List<T> list = new ArrayList<>();
for (T spi : serviceLoader) {
list.add(spi);
}
return list;
} catch (Throwable t) {
RecordLog.warn("[SpiLoader] ERROR: loadInstanceListSorted failed", t);
t.printStackTrace();
return new ArrayList<>();
}
}

/**
* Load the sorted SPI instance list for provided SPI interface.
*
* @param clazz class of the SPI
* @param <T> SPI type


Loading…
취소
저장