Use ServiceLoaderUtil to create SPI ServiceLoader

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
Eric Zhao 2019-10-16 00:53:50 +08:00
parent 8d5654f727
commit b0905547ff
5 changed files with 21 additions and 47 deletions

View File

@ -15,13 +15,10 @@
*/
package com.alibaba.csp.sentinel.cluster.server;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
import com.alibaba.csp.sentinel.cluster.TokenService;
import com.alibaba.csp.sentinel.cluster.flow.DefaultTokenService;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.util.SpiLoader;
/**
* @author Eric Zhao
@ -31,8 +28,6 @@ public final class TokenServiceProvider {
private static TokenService service = null;
private static final ServiceLoader<TokenService> LOADER = ServiceLoader.load(TokenService.class);
static {
resolveTokenServiceSpi();
}
@ -42,24 +37,12 @@ public final class TokenServiceProvider {
}
private static void resolveTokenServiceSpi() {
boolean hasOther = false;
List<TokenService> list = new ArrayList<TokenService>();
for (TokenService service : LOADER) {
if (service.getClass() != DefaultTokenService.class) {
hasOther = true;
list.add(service);
}
}
if (hasOther) {
// Pick the first.
service = list.get(0);
service = SpiLoader.loadFirstInstanceOrDefault(TokenService.class, DefaultTokenService.class);
if (service != null) {
RecordLog.info("[TokenServiceProvider] Global token service resolved: "
+ service.getClass().getCanonicalName());
} else {
// No custom token service, using default.
service = new DefaultTokenService();
RecordLog.warn("[TokenServiceProvider] Unable to resolve TokenService: no SPI found");
}
RecordLog.info("[TokenServiceProvider] Global token service resolved: "
+ service.getClass().getCanonicalName());
}
}

View File

@ -20,6 +20,7 @@ import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;
import com.alibaba.csp.sentinel.cluster.annotation.RequestType;
import com.alibaba.csp.sentinel.spi.ServiceLoaderUtil;
import com.alibaba.csp.sentinel.util.AssertUtil;
/**
@ -30,7 +31,8 @@ public final class RequestProcessorProvider {
private static final Map<Integer, RequestProcessor> PROCESSOR_MAP = new ConcurrentHashMap<>();
private static final ServiceLoader<RequestProcessor> SERVICE_LOADER = ServiceLoader.load(RequestProcessor.class);
private static final ServiceLoader<RequestProcessor> SERVICE_LOADER = ServiceLoaderUtil.getServiceLoader(
RequestProcessor.class);
static {
loadAndInit();
@ -71,6 +73,5 @@ public final class RequestProcessorProvider {
PROCESSOR_MAP.put(type, processor);
}
private RequestProcessorProvider() {}
}

View File

@ -21,6 +21,7 @@ import java.util.ServiceLoader;
import java.util.concurrent.atomic.AtomicBoolean;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.spi.ServiceLoaderUtil;
/**
* Load registered init functions and execute in order.
@ -42,7 +43,7 @@ public final class InitExecutor {
return;
}
try {
ServiceLoader<InitFunc> loader = ServiceLoader.load(InitFunc.class);
ServiceLoader<InitFunc> loader = ServiceLoaderUtil.getServiceLoader(InitFunc.class);
List<OrderWrapper> initList = new ArrayList<OrderWrapper>();
for (InitFunc initFunc : loader) {
RecordLog.info("[InitExecutor] Found init func: " + initFunc.getClass().getCanonicalName());

View File

@ -17,7 +17,7 @@ package com.alibaba.csp.sentinel.slotchain;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.slots.DefaultSlotChainBuilder;
import java.util.ServiceLoader;
import com.alibaba.csp.sentinel.util.SpiLoader;
/**
* A provider for creating slot chains via resolved slot chain builder SPI.
@ -29,8 +29,6 @@ public final class SlotChainProvider {
private static volatile SlotChainBuilder slotChainBuilder = null;
private static final ServiceLoader<SlotChainBuilder> LOADER = ServiceLoader.load(SlotChainBuilder.class);
/**
* The load and pick process is not thread-safe, but it's okay since the method should be only invoked
* via {@code lookProcessChain} in {@link com.alibaba.csp.sentinel.CtSph} under lock.
@ -42,30 +40,19 @@ public final class SlotChainProvider {
return slotChainBuilder.build();
}
resolveSlotChainBuilder();
// Resolve the slot chain builder SPI.
slotChainBuilder = SpiLoader.loadFirstInstanceOrDefault(SlotChainBuilder.class, DefaultSlotChainBuilder.class);
if (slotChainBuilder == null) {
// Should not go through here.
RecordLog.warn("[SlotChainProvider] Wrong state when resolving slot chain builder, using default");
slotChainBuilder = new DefaultSlotChainBuilder();
} else {
RecordLog.info("[SlotChainProvider] Global slot chain builder resolved: "
+ slotChainBuilder.getClass().getCanonicalName());
}
return slotChainBuilder.build();
}
private static void resolveSlotChainBuilder() {
for (SlotChainBuilder builder : LOADER) {
if (builder.getClass() != DefaultSlotChainBuilder.class) {
slotChainBuilder = builder;
break;
}
}
if (slotChainBuilder == null){
// No custom builder, using default.
slotChainBuilder = new DefaultSlotChainBuilder();
}
RecordLog.info("[SlotChainProvider] Global slot chain builder resolved: "
+ slotChainBuilder.getClass().getCanonicalName());
}
private SlotChainProvider() {}
}

View File

@ -21,6 +21,7 @@ import java.util.Map;
import java.util.ServiceLoader;
import com.alibaba.csp.sentinel.command.annotation.CommandMapping;
import com.alibaba.csp.sentinel.spi.ServiceLoaderUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
/**
@ -30,7 +31,8 @@ import com.alibaba.csp.sentinel.util.StringUtil;
*/
public class CommandHandlerProvider implements Iterable<CommandHandler> {
private final ServiceLoader<CommandHandler> serviceLoader = ServiceLoader.load(CommandHandler.class);
private final ServiceLoader<CommandHandler> serviceLoader = ServiceLoaderUtil.getServiceLoader(
CommandHandler.class);
/**
* Get all command handlers annotated with {@link CommandMapping} with command name.