瀏覽代碼

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…
取消
儲存