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; } }
@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(); }
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; }
@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 }
@Test(expected = HystrixCachingException.class) public void testCacheResultMethodWithWrongCacheKeyMethodSignature_givenWrongCacheKeyMethod_shouldThrowException() throws NoSuchMethodException { // given TestCacheClass testCacheClass = new TestCacheClass(); String param1 = "val_1"; MetaHolder metaHolder = MetaHolder.builder() .method(TestCacheClass.class.getMethod("cacheResultMethodWithWrongCacheKeyMethodSignature", String.class)) .args(new Object[]{param1}) .obj(testCacheClass).build(); // when CacheInvocationContext<CacheResult> context = CacheInvocationContextFactory.createCacheResultInvocationContext(metaHolder); // then expected HystrixCachingException }
@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); }
@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)); }
@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()); }
fMethod.setAccessible(true); HystrixCommand hystrixCommand = fMethod.getAnnotation(HystrixCommand.class); MetaHolder fmMetaHolder = MetaHolder.builder() .obj(metaHolder.getObj()) .method(fMethod) fallbackAction = new LazyCommandExecutionAction(fmMetaHolder); } else { MetaHolder fmMetaHolder = MetaHolder.builder() .obj(metaHolder.getObj()) .defaultFallback(fallbackMethod.isDefault())
@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); }
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(); }
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(); }
@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); }
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; } }
@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(); }
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; }
fMethod.setAccessible(true); HystrixCommand hystrixCommand = fMethod.getAnnotation(HystrixCommand.class); MetaHolder fmMetaHolder = MetaHolder.builder() .obj(metaHolder.getObj()) .method(fMethod) fallbackAction = new LazyCommandExecutionAction(fmMetaHolder); } else { MetaHolder fmMetaHolder = MetaHolder.builder() .obj(metaHolder.getObj()) .defaultFallback(fallbackMethod.isDefault())
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(); }