Bladeren bron

Add loadInstanceList support for SpiLoader

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 5 jaren geleden
bovenliggende
commit
c316211faf
1 gewijzigde bestanden met toevoegingen van 31 en 1 verwijderingen
  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 Bestand weergeven

@@ -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 clazz class of the SPI
* @param <T> SPI type * @param <T> SPI type


Laden…
Annuleren
Opslaan