use of com.github.df.restypass.command.RestyCommandConfig in project RestyPass by darren-fu.
the class RestyFallbackExecutor method execute.
@Override
public Object execute(RestyCommand restyCommand) throws FallbackException {
RestyCommandConfig config = restyCommand.getRestyCommandConfig();
String serviceName = restyCommand.getServiceName();
Class fallbackClass = config.getFallbackClass();
// 获取降级服务实例
Object fallbackObj = fallbackObjMap.get(serviceName);
if (fallbackObj == null) {
Object fo = getFallbackObject(restyCommand);
fallbackObjMap.putIfAbsent(serviceName, fo);
fallbackObj = fallbackObjMap.get(serviceName);
}
if (fallbackObj == null || fallbackObj == RestyService.Noop.noop) {
throw new FallbackException("无法获取指定的降级服务实例:" + fallbackClass);
}
try {
return findAndInvokeMethodInFallbackClass(restyCommand, fallbackObj);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
throw new FallbackException(e);
}
}
use of com.github.df.restypass.command.RestyCommandConfig in project RestyPass by darren-fu.
the class RestyFallbackExecutor method getFallbackObject.
private Object getFallbackObject(RestyCommand restyCommand) {
Object t = null;
RestyCommandConfig config = restyCommand.getRestyCommandConfig();
Class fallbackClz = config.getFallbackClass();
String fallbackBean = config.getFallbackBean();
// 优先按照bean name从spring注入指定bean
if (StringUtils.isNotEmpty(fallbackBean)) {
if (this.applicationContext != null) {
t = this.applicationContext.getBean(fallbackBean);
if (t != null && log.isDebugEnabled()) {
log.debug("注入降级服务:{}", t.getClass());
}
}
} else if (fallbackClz != null && fallbackClz != RestyService.Noop.class) {
// 否则初始化指定class
t = ClassTools.instance(fallbackClz);
if (t != null && log.isDebugEnabled()) {
log.debug("使用降级服务:{}", fallbackClz);
}
}
return t == null ? RestyService.Noop.noop : t;
}
Aggregations