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;
}
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;
}
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());
}
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
}
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);
}
Aggregations