@Test public void testCreateCacheResultInvocationContext_givenMethodAnnotatedWithCacheResult_shouldCreateCorrectCacheKeyInvocationContext() throws NoSuchMethodException { // given TestCacheClass testCacheClass = new TestCacheClass(); String param1 = "val_1"; String param2 = "val_2"; Integer param3 = 3; MetaHolder metaHolder = MetaHolder.builder() .method(TestCacheClass.class.getMethod("cacheResultMethod", String.class, String.class, Integer.class)) .args(new Object[]{param1, param2, param3}) .obj(testCacheClass).build(); // when CacheInvocationContext<CacheResult> context = CacheInvocationContextFactory.createCacheResultInvocationContext(metaHolder); // then assertNotNull(context.getKeyParameters()); assertEquals(2, context.getKeyParameters().size()); assertEquals(String.class, context.getKeyParameters().get(0).getRawType()); assertEquals(0, context.getKeyParameters().get(0).getPosition()); assertEquals(param1, context.getKeyParameters().get(0).getValue()); assertTrue(isAnnotationPresent(context.getKeyParameters().get(0), CacheKey.class)); assertEquals(Integer.class, context.getKeyParameters().get(1).getRawType()); assertEquals(2, context.getKeyParameters().get(1).getPosition()); assertEquals(param3, context.getKeyParameters().get(1).getValue()); assertTrue(isAnnotationPresent(context.getKeyParameters().get(1), CacheKey.class)); }
builder.ajcMethod(getAjcMethodAroundAdvice(obj.getClass(), batchCommandMethod.getName(), List.class)); builder.hystrixCollapser(hystrixCollapser); builder.defaultCollapserKey(collapserMethod.getName()); builder.collapserExecutionType(ExecutionType.getExecutionType(collapserReturnType)); builder.defaultCommandKey(batchCommandMethod.getName()); builder.hystrixCommand(hystrixCommand); builder.executionType(ExecutionType.getExecutionType(batchReturnType)); builder.observable(observable); FallbackMethod fallbackMethod = MethodProvider.getInstance().getFallbackMethod(obj.getClass(), batchCommandMethod); if (fallbackMethod.isPresent()) { fallbackMethod.validateReturnType(batchCommandMethod); builder .fallbackMethod(fallbackMethod.getMethod()) .fallbackExecutionType(ExecutionType.getExecutionType(fallbackMethod.getMethod().getReturnType())); return builder.build();
builder.ajcMethod(getAjcMethodAroundAdvice(obj.getClass(), batchCommandMethod.getName(), List.class)); builder.hystrixCollapser(hystrixCollapser); builder.defaultCollapserKey(collapserMethod.getName()); builder.collapserExecutionType(ExecutionType.getExecutionType(collapserReturnType)); builder.defaultCommandKey(batchCommandMethod.getName()); builder.hystrixCommand(hystrixCommand); builder.executionType(ExecutionType.getExecutionType(batchReturnType)); builder.observable(observable); FallbackMethod fallbackMethod = MethodProvider.getInstance().getFallbackMethod(obj.getClass(), batchCommandMethod); if (fallbackMethod.isPresent()) { fallbackMethod.validateReturnType(batchCommandMethod); builder .fallbackMethod(fallbackMethod.getMethod()) .fallbackExecutionType(ExecutionType.getExecutionType(fallbackMethod.getMethod().getReturnType())); return builder.build();
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())
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; }
private MetaHolder createCopy(MetaHolder source, ExecutionType executionType, Object[] args) { return MetaHolder.builder() .obj(source.getObj()) .method(source.getMethod()) .executionType(executionType) .ajcMethod(source.getAjcMethod()) .fallbackExecutionType(source.getFallbackExecutionType()) .extendedParentFallback(source.isExtendedParentFallback()) .extendedFallback(source.isExtendedFallback()) .args(args) .observable(source.isObservable()) .observableExecutionMode(source.getObservableExecutionMode()) .defaultCollapserKey(source.getDefaultCollapserKey()) .defaultCommandKey(source.getDefaultCommandKey()) .defaultGroupKey(source.getDefaultGroupKey()) .defaultThreadPoolKey(source.getDefaultThreadPoolKey()) .defaultProperties(source.getDefaultProperties().orNull()) .hystrixCollapser(source.getHystrixCollapser()) .hystrixCommand(source.getHystrixCommand()).build(); }
private MetaHolder createCopy(MetaHolder source, ExecutionType executionType) { return MetaHolder.builder() .obj(source.getObj()) .method(source.getMethod()) .ajcMethod(source.getAjcMethod()) .fallbackExecutionType(source.getFallbackExecutionType()) .extendedFallback(source.isExtendedFallback()) .extendedParentFallback(source.isExtendedParentFallback()) .executionType(executionType) .args(source.getArgs()) .observable(source.isObservable()) .observableExecutionMode(source.getObservableExecutionMode()) .defaultCollapserKey(source.getDefaultCollapserKey()) .defaultCommandKey(source.getDefaultCommandKey()) .defaultGroupKey(source.getDefaultGroupKey()) .defaultThreadPoolKey(source.getDefaultThreadPoolKey()) .defaultProperties(source.getDefaultProperties().orNull()) .hystrixCollapser(source.getHystrixCollapser()) .hystrixCommand(source.getHystrixCommand()).build(); }
@Override public MetaHolder create(Object proxy, Method method, Object obj, Object[] args, final ProceedingJoinPoint joinPoint) { HystrixCommand hystrixCommand = method.getAnnotation(HystrixCommand.class); ExecutionType executionType = ExecutionType.getExecutionType(method.getReturnType()); MetaHolder.Builder builder = metaHolderBuilder(proxy, method, obj, args, joinPoint); if (isCompileWeaving()) { builder.ajcMethod(getAjcMethodFromTarget(joinPoint)); } return builder.defaultCommandKey(method.getName()) .hystrixCommand(hystrixCommand) .observableExecutionMode(hystrixCommand.observableExecutionMode()) .executionType(executionType) .observable(ExecutionType.OBSERVABLE == executionType) .build(); } }
@Around("cacheRemoveAnnotationPointcut()") public Object methodsAnnotatedWithCacheRemove(final ProceedingJoinPoint joinPoint) throws Throwable { Method method = getMethodFromTarget(joinPoint); Object obj = joinPoint.getTarget(); Object[] args = joinPoint.getArgs(); Validate.notNull(method, "failed to get method from joinPoint: %s", joinPoint); MetaHolder metaHolder = MetaHolder.builder() .args(args).method(method).obj(obj) .executionType(ExecutionType.SYNCHRONOUS) .ajcMethod(isCompileWeaving() ? getAjcMethodAroundAdvice(obj.getClass(), method) : null) .build(); CacheInvocationContext<CacheRemove> context = CacheInvocationContextFactory .createCacheRemoveInvocationContext(metaHolder); HystrixRequestCacheManager.getInstance().clearCache(context); return joinPoint.proceed(); }
public static Builder builder() { return new Builder(); }
MetaHolder.Builder metaHolderBuilder(Object proxy, Method method, Object obj, Object[] args, final ProceedingJoinPoint joinPoint) { MetaHolder.Builder builder = MetaHolder.builder() .args(args).method(method).obj(obj).proxyObj(proxy) .joinPoint(joinPoint); setFallbackMethod(builder, obj.getClass(), method); builder = setDefaultProperties(builder, obj.getClass(), joinPoint); return builder; } }
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())
@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()); }
@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); }
@Test public void testGenerateCacheKey_givenUser_shouldReturnCorrectCacheKey() throws NoSuchMethodException { // given TestCacheClass testCacheClass = new TestCacheClass(); String id = "1"; User user = new User(); user.setId(id); Profile profile = new Profile("user name"); user.setProfile(profile); String expectedKey = id + user.getProfile().getName(); 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); }
private MetaHolder createCopy(MetaHolder source, ExecutionType executionType, Object[] args) { return MetaHolder.builder() .obj(source.getObj()) .method(source.getMethod()) .executionType(executionType) .ajcMethod(source.getAjcMethod()) .fallbackExecutionType(source.getFallbackExecutionType()) .extendedParentFallback(source.isExtendedParentFallback()) .extendedFallback(source.isExtendedFallback()) .args(args) .observable(source.isObservable()) .observableExecutionMode(source.getObservableExecutionMode()) .defaultCollapserKey(source.getDefaultCollapserKey()) .defaultCommandKey(source.getDefaultCommandKey()) .defaultGroupKey(source.getDefaultGroupKey()) .defaultThreadPoolKey(source.getDefaultThreadPoolKey()) .defaultProperties(source.getDefaultProperties().orNull()) .hystrixCollapser(source.getHystrixCollapser()) .hystrixCommand(source.getHystrixCommand()).build(); }
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; }
private MetaHolder createCopy(MetaHolder source, ExecutionType executionType) { return MetaHolder.builder() .obj(source.getObj()) .method(source.getMethod()) .ajcMethod(source.getAjcMethod()) .fallbackExecutionType(source.getFallbackExecutionType()) .extendedFallback(source.isExtendedFallback()) .extendedParentFallback(source.isExtendedParentFallback()) .executionType(executionType) .args(source.getArgs()) .observable(source.isObservable()) .observableExecutionMode(source.getObservableExecutionMode()) .defaultCollapserKey(source.getDefaultCollapserKey()) .defaultCommandKey(source.getDefaultCommandKey()) .defaultGroupKey(source.getDefaultGroupKey()) .defaultThreadPoolKey(source.getDefaultThreadPoolKey()) .defaultProperties(source.getDefaultProperties().orNull()) .hystrixCollapser(source.getHystrixCollapser()) .hystrixCommand(source.getHystrixCommand()).build(); }
@Test public void testGenerateCacheKey_givenCacheKeyMethodWithNoArguments_shouldReturnEmptyCacheKey() throws NoSuchMethodException { // given TestCacheClass testCacheClass = new TestCacheClass(); MetaHolder metaHolder = MetaHolder.builder() .method(TestCacheClass.class.getMethod("cacheResultMethod")) .args(new Object[]{}) .obj(testCacheClass).build(); CacheInvocationContext<CacheResult> context = CacheInvocationContextFactory.createCacheResultInvocationContext(metaHolder); HystrixCacheKeyGenerator keyGenerator = HystrixCacheKeyGenerator.getInstance(); // when HystrixGeneratedCacheKey actual = keyGenerator.generateCacheKey(context); // then assertEquals(DefaultHystrixGeneratedCacheKey.EMPTY, actual); }
@Override public MetaHolder create(Object proxy, Method method, Object obj, Object[] args, final ProceedingJoinPoint joinPoint) { HystrixCommand hystrixCommand = method.getAnnotation(HystrixCommand.class); ExecutionType executionType = ExecutionType.getExecutionType(method.getReturnType()); MetaHolder.Builder builder = metaHolderBuilder(proxy, method, obj, args, joinPoint); if (isCompileWeaving()) { builder.ajcMethod(getAjcMethodFromTarget(joinPoint)); } return builder.defaultCommandKey(method.getName()) .hystrixCommand(hystrixCommand) .observableExecutionMode(hystrixCommand.observableExecutionMode()) .executionType(executionType) .observable(ExecutionType.OBSERVABLE == executionType) .build(); } }