Search in sources :

Example 1 with HystrixCommand

use of com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand in project Hystrix by Netflix.

the class HystrixCommandAspect method methodsAnnotatedWithHystrixCommand.

@Around("hystrixCommandAnnotationPointcut() || hystrixCollapserAnnotationPointcut()")
public Object methodsAnnotatedWithHystrixCommand(final ProceedingJoinPoint joinPoint) throws Throwable {
    Method method = getMethodFromTarget(joinPoint);
    Validate.notNull(method, "failed to get method from joinPoint: %s", joinPoint);
    if (method.isAnnotationPresent(HystrixCommand.class) && method.isAnnotationPresent(HystrixCollapser.class)) {
        throw new IllegalStateException("method cannot be annotated with HystrixCommand and HystrixCollapser " + "annotations at the same time");
    }
    MetaHolderFactory metaHolderFactory = META_HOLDER_FACTORY_MAP.get(HystrixPointcutType.of(method));
    MetaHolder metaHolder = metaHolderFactory.create(joinPoint);
    HystrixInvokable invokable = HystrixCommandFactory.getInstance().create(metaHolder);
    ExecutionType executionType = metaHolder.isCollapserAnnotationPresent() ? metaHolder.getCollapserExecutionType() : metaHolder.getExecutionType();
    Object result;
    try {
        if (!metaHolder.isObservable()) {
            result = CommandExecutor.execute(invokable, executionType, metaHolder);
        } else {
            result = executeObservable(invokable, executionType, metaHolder);
        }
    } catch (HystrixBadRequestException e) {
        throw e.getCause();
    } catch (HystrixRuntimeException e) {
        throw hystrixRuntimeExceptionToThrowable(metaHolder, e);
    }
    return result;
}
Also used : HystrixCommand(com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand) HystrixCollapser(com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) MetaHolder(com.netflix.hystrix.contrib.javanica.command.MetaHolder) ExecutionType(com.netflix.hystrix.contrib.javanica.command.ExecutionType) HystrixInvokable(com.netflix.hystrix.HystrixInvokable) AopUtils.getDeclaredMethod(com.netflix.hystrix.contrib.javanica.utils.AopUtils.getDeclaredMethod) Method(java.lang.reflect.Method) FallbackMethod(com.netflix.hystrix.contrib.javanica.utils.FallbackMethod) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) Around(org.aspectj.lang.annotation.Around)

Example 2 with HystrixCommand

use of com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand in project Hystrix by Netflix.

the class HystrixCommandBuilderFactory method createFallbackAction.

private CommandAction createFallbackAction(MetaHolder metaHolder) {
    FallbackMethod fallbackMethod = MethodProvider.getInstance().getFallbackMethod(metaHolder.getObj().getClass(), metaHolder.getMethod(), metaHolder.isExtendedFallback());
    fallbackMethod.validateReturnType(metaHolder.getMethod());
    CommandAction fallbackAction = null;
    if (fallbackMethod.isPresent()) {
        Method fMethod = fallbackMethod.getMethod();
        Object[] args = fallbackMethod.isDefault() ? new Object[0] : metaHolder.getArgs();
        if (fallbackMethod.isCommand()) {
            fMethod.setAccessible(true);
            HystrixCommand hystrixCommand = fMethod.getAnnotation(HystrixCommand.class);
            MetaHolder fmMetaHolder = MetaHolder.builder().obj(metaHolder.getObj()).method(fMethod).ajcMethod(getAjcMethod(metaHolder.getObj(), fMethod)).args(args).fallback(true).defaultFallback(fallbackMethod.isDefault()).defaultCollapserKey(metaHolder.getDefaultCollapserKey()).fallbackMethod(fMethod).extendedFallback(fallbackMethod.isExtended()).fallbackExecutionType(fallbackMethod.getExecutionType()).extendedParentFallback(metaHolder.isExtendedFallback()).observable(ExecutionType.OBSERVABLE == fallbackMethod.getExecutionType()).defaultCommandKey(fMethod.getName()).defaultGroupKey(metaHolder.getDefaultGroupKey()).defaultThreadPoolKey(metaHolder.getDefaultThreadPoolKey()).defaultProperties(metaHolder.getDefaultProperties().orNull()).hystrixCollapser(metaHolder.getHystrixCollapser()).observableExecutionMode(hystrixCommand.observableExecutionMode()).hystrixCommand(hystrixCommand).build();
            fallbackAction = new LazyCommandExecutionAction(fmMetaHolder);
        } else {
            MetaHolder fmMetaHolder = MetaHolder.builder().obj(metaHolder.getObj()).defaultFallback(fallbackMethod.isDefault()).method(fMethod).fallbackExecutionType(ExecutionType.SYNCHRONOUS).extendedFallback(fallbackMethod.isExtended()).extendedParentFallback(metaHolder.isExtendedFallback()).ajcMethod(// if fallback method isn't annotated with command annotation then we don't need to get ajc method for this
            null).args(args).build();
            fallbackAction = new MethodExecutionAction(fmMetaHolder.getObj(), fMethod, fmMetaHolder.getArgs(), fmMetaHolder);
        }
    }
    return fallbackAction;
}
Also used : HystrixCommand(com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand) FallbackMethod(com.netflix.hystrix.contrib.javanica.utils.FallbackMethod) Method(java.lang.reflect.Method) FallbackMethod(com.netflix.hystrix.contrib.javanica.utils.FallbackMethod)

Aggregations

HystrixCommand (com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand)2 FallbackMethod (com.netflix.hystrix.contrib.javanica.utils.FallbackMethod)2 Method (java.lang.reflect.Method)2 HystrixInvokable (com.netflix.hystrix.HystrixInvokable)1 HystrixCollapser (com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser)1 ExecutionType (com.netflix.hystrix.contrib.javanica.command.ExecutionType)1 MetaHolder (com.netflix.hystrix.contrib.javanica.command.MetaHolder)1 AopUtils.getDeclaredMethod (com.netflix.hystrix.contrib.javanica.utils.AopUtils.getDeclaredMethod)1 HystrixBadRequestException (com.netflix.hystrix.exception.HystrixBadRequestException)1 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)1 Around (org.aspectj.lang.annotation.Around)1