Search in sources :

Example 1 with RestyCommandConfig

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);
    }
}
Also used : RestyCommandConfig(com.github.df.restypass.command.RestyCommandConfig) FallbackException(com.github.df.restypass.exception.execute.FallbackException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with RestyCommandConfig

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;
}
Also used : RestyCommandConfig(com.github.df.restypass.command.RestyCommandConfig) RestyService(com.github.df.restypass.annotation.RestyService)

Aggregations

RestyCommandConfig (com.github.df.restypass.command.RestyCommandConfig)2 RestyService (com.github.df.restypass.annotation.RestyService)1 FallbackException (com.github.df.restypass.exception.execute.FallbackException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1