Search in sources :

Example 1 with MetaHolder

use of com.netflix.hystrix.contrib.javanica.command.MetaHolder in project Hystrix by Netflix.

the class CacheInvocationContextFactory method createCacheKeyAction.

private static MethodExecutionAction createCacheKeyAction(String method, MetaHolder metaHolder) {
    MethodExecutionAction cacheKeyAction = null;
    if (StringUtils.isNotBlank(method)) {
        Method cacheKeyMethod = getDeclaredMethod(metaHolder.getObj().getClass(), method, metaHolder.getMethod().getParameterTypes());
        if (cacheKeyMethod == null) {
            throw new HystrixCachingException("method with name '" + method + "' doesn't exist in class '" + metaHolder.getObj().getClass() + "'");
        }
        if (!cacheKeyMethod.getReturnType().equals(String.class)) {
            throw new HystrixCachingException("return type of cacheKey method must be String. Method: '" + method + "', Class: '" + metaHolder.getObj().getClass() + "'");
        }
        MetaHolder cMetaHolder = MetaHolder.builder().obj(metaHolder.getObj()).method(cacheKeyMethod).args(metaHolder.getArgs()).build();
        cacheKeyAction = new MethodExecutionAction(cMetaHolder.getObj(), cacheKeyMethod, cMetaHolder.getArgs(), cMetaHolder);
    }
    return cacheKeyAction;
}
Also used : HystrixCachingException(com.netflix.hystrix.contrib.javanica.exception.HystrixCachingException) MetaHolder(com.netflix.hystrix.contrib.javanica.command.MetaHolder) MethodExecutionAction(com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction) AopUtils.getDeclaredMethod(com.netflix.hystrix.contrib.javanica.utils.AopUtils.getDeclaredMethod) Method(java.lang.reflect.Method)

Example 2 with MetaHolder

use of com.netflix.hystrix.contrib.javanica.command.MetaHolder 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 3 with MetaHolder

use of com.netflix.hystrix.contrib.javanica.command.MetaHolder in project Hystrix by Netflix.

the class CacheInvocationContextFactoryTest method testCreateCacheRemoveInvocationContext_givenMethodAnnotatedWithCacheRemove_shouldCreateCorrectCacheKeyInvocationContext.

@Test
public void testCreateCacheRemoveInvocationContext_givenMethodAnnotatedWithCacheRemove_shouldCreateCorrectCacheKeyInvocationContext() throws NoSuchMethodException {
    // given
    TestCacheClass testCacheClass = new TestCacheClass();
    String param1 = "val_1";
    MetaHolder metaHolder = MetaHolder.builder().method(TestCacheClass.class.getMethod("cacheRemoveMethod", String.class)).args(new Object[] { param1 }).obj(testCacheClass).build();
    // when
    CacheInvocationContext<CacheRemove> context = CacheInvocationContextFactory.createCacheRemoveInvocationContext(metaHolder);
    // then
    assertNotNull(context.getKeyParameters());
    assertEquals(1, context.getKeyParameters().size());
    CacheInvocationParameter actual = context.getKeyParameters().get(0);
    assertEquals(String.class, actual.getRawType());
    assertEquals(param1, actual.getValue());
    assertEquals(0, actual.getPosition());
}
Also used : CacheRemove(com.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove) MetaHolder(com.netflix.hystrix.contrib.javanica.command.MetaHolder) Test(org.junit.Test)

Example 4 with MetaHolder

use of com.netflix.hystrix.contrib.javanica.command.MetaHolder in project Hystrix by Netflix.

the class CacheInvocationContextFactoryTest method testCacheResultMethodWithCacheKeyMethodWithWrongReturnType_givenCacheKeyMethodWithWrongReturnType_shouldThrowException.

@Test(expected = HystrixCachingException.class)
public void testCacheResultMethodWithCacheKeyMethodWithWrongReturnType_givenCacheKeyMethodWithWrongReturnType_shouldThrowException() throws NoSuchMethodException {
    // given
    TestCacheClass testCacheClass = new TestCacheClass();
    String param1 = "val_1";
    MetaHolder metaHolder = MetaHolder.builder().method(TestCacheClass.class.getMethod("cacheResultMethodWithCacheKeyMethodWithWrongReturnType", String.class, String.class)).args(new Object[] { param1 }).obj(testCacheClass).build();
    // when
    CacheInvocationContext<CacheResult> context = CacheInvocationContextFactory.createCacheResultInvocationContext(metaHolder);
    System.out.println(context);
// then expected HystrixCachingException
}
Also used : MetaHolder(com.netflix.hystrix.contrib.javanica.command.MetaHolder) CacheResult(com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult) Test(org.junit.Test)

Example 5 with MetaHolder

use of com.netflix.hystrix.contrib.javanica.command.MetaHolder in project Hystrix by Netflix.

the class HystrixCacheKeyGeneratorTest method testGenerateCacheKey_givenUserWithNullProfile_shouldReturnCorrectCacheKey.

@Test
public void testGenerateCacheKey_givenUserWithNullProfile_shouldReturnCorrectCacheKey() throws NoSuchMethodException {
    // given
    TestCacheClass testCacheClass = new TestCacheClass();
    String id = "1";
    User user = new User();
    user.setId(id);
    user.setProfile(null);
    String expectedKey = id;
    MetaHolder metaHolder = MetaHolder.builder().method(TestCacheClass.class.getMethod("cacheResultMethod", String.class, User.class)).args(new Object[] { id, user }).obj(testCacheClass).build();
    CacheInvocationContext<CacheResult> context = CacheInvocationContextFactory.createCacheResultInvocationContext(metaHolder);
    HystrixCacheKeyGenerator keyGenerator = HystrixCacheKeyGenerator.getInstance();
    // when
    String actual = keyGenerator.generateCacheKey(context).getCacheKey();
    // then
    assertEquals(expectedKey, actual);
}
Also used : MetaHolder(com.netflix.hystrix.contrib.javanica.command.MetaHolder) CacheResult(com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult) Test(org.junit.Test)

Aggregations

MetaHolder (com.netflix.hystrix.contrib.javanica.command.MetaHolder)10 Test (org.junit.Test)7 CacheResult (com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult)6 Method (java.lang.reflect.Method)3 CacheRemove (com.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove)2 AopUtils.getDeclaredMethod (com.netflix.hystrix.contrib.javanica.utils.AopUtils.getDeclaredMethod)2 Around (org.aspectj.lang.annotation.Around)2 HystrixInvokable (com.netflix.hystrix.HystrixInvokable)1 HystrixCollapser (com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser)1 HystrixCommand (com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand)1 CacheKey (com.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey)1 ExecutionType (com.netflix.hystrix.contrib.javanica.command.ExecutionType)1 MethodExecutionAction (com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction)1 HystrixCachingException (com.netflix.hystrix.contrib.javanica.exception.HystrixCachingException)1 FallbackMethod (com.netflix.hystrix.contrib.javanica.utils.FallbackMethod)1 HystrixBadRequestException (com.netflix.hystrix.exception.HystrixBadRequestException)1 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)1