@Bean(name = "jCacheInterceptor") @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public JCacheInterceptor cacheInterceptor() { JCacheInterceptor interceptor = new JCacheInterceptor(this.errorHandler); interceptor.setCacheOperationSource(cacheOperationSource()); return interceptor; }
@Override @Nullable public Object invoke(final MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod(); CacheOperationInvoker aopAllianceInvoker = () -> { try { return invocation.proceed(); } catch (Throwable ex) { throw new CacheOperationInvoker.ThrowableWrapper(ex); } }; try { return execute(aopAllianceInvoker, invocation.getThis(), method, invocation.getArguments()); } catch (CacheOperationInvoker.ThrowableWrapper th) { throw th.getOriginal(); } }
protected JCacheInterceptor createInterceptor(JCacheOperationSource source) { JCacheInterceptor interceptor = new JCacheInterceptor(); interceptor.setCacheOperationSource(source); interceptor.afterPropertiesSet(); return interceptor; }
@Bean public JCacheInterceptor jCacheInterceptor(JCacheOperationSource cacheOperationSource) { JCacheInterceptor cacheInterceptor = new TestCacheInterceptor(); cacheInterceptor.setCacheOperationSource(cacheOperationSource); return cacheInterceptor; } }
@Test public void testCacheErrorHandler() { JCacheInterceptor ci = ctx.getBean(JCacheInterceptor.class); assertSame(ctx.getBean("errorHandler", CacheErrorHandler.class), ci.getErrorHandler()); }
@Override protected Object invokeOperation(CacheOperationInvoker invoker) { try { return super.invokeOperation(invoker); } catch (CacheOperationInvoker.ThrowableWrapper e) { Throwable original = e.getOriginal(); if (original.getClass() == UnsupportedOperationException.class) { return 55L; } else { throw new CacheOperationInvoker.ThrowableWrapper( new RuntimeException("wrapping original", original)); } } } }
@Test public void fullCachingConfig() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(FullCachingConfig.class); DefaultJCacheOperationSource cos = context.getBean(DefaultJCacheOperationSource.class); assertSame(context.getBean(KeyGenerator.class), cos.getKeyGenerator()); assertSame(context.getBean("cacheResolver", CacheResolver.class), cos.getCacheResolver()); assertSame(context.getBean("exceptionCacheResolver", CacheResolver.class), cos.getExceptionCacheResolver()); JCacheInterceptor interceptor = context.getBean(JCacheInterceptor.class); assertSame(context.getBean("errorHandler", CacheErrorHandler.class), interceptor.getErrorHandler()); }
@Bean(name = "jCacheInterceptor") @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public JCacheInterceptor cacheInterceptor() { JCacheInterceptor interceptor = new JCacheInterceptor(this.errorHandler); interceptor.setCacheOperationSource(cacheOperationSource()); return interceptor; }
@Override @Nullable public Object invoke(final MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod(); CacheOperationInvoker aopAllianceInvoker = () -> { try { return invocation.proceed(); } catch (Throwable ex) { throw new CacheOperationInvoker.ThrowableWrapper(ex); } }; try { return execute(aopAllianceInvoker, invocation.getThis(), method, invocation.getArguments()); } catch (CacheOperationInvoker.ThrowableWrapper th) { throw th.getOriginal(); } }
@Bean(name = "jCacheInterceptor") @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public JCacheInterceptor cacheInterceptor() { JCacheInterceptor interceptor = new JCacheInterceptor(); interceptor.setCacheOperationSource(cacheOperationSource()); if (this.errorHandler != null) { interceptor.setErrorHandler(this.errorHandler); } return interceptor; }
@Test public void cacheResultReturnsProperType() throws Throwable { JCacheInterceptor interceptor = createInterceptor(createOperationSource( cacheManager, defaultCacheResolver, defaultExceptionCacheResolver, defaultKeyGenerator)); AnnotatedJCacheableService service = new AnnotatedJCacheableService(cacheManager.getCache("default")); Method method = ReflectionUtils.findMethod(AnnotatedJCacheableService.class, "cache", String.class); CacheOperationInvoker invoker = new DummyInvoker(0L); Object execute = interceptor.execute(invoker, service, method, new Object[] {"myId"}); assertNotNull("result cannot be null.", execute); assertEquals("Wrong result type", Long.class, execute.getClass()); assertEquals("Wrong result", 0L, execute); }
@Test public void severalCachesNotSupported() { JCacheInterceptor interceptor = createInterceptor(createOperationSource( cacheManager, new NamedCacheResolver(cacheManager, "default", "simpleCache"), defaultExceptionCacheResolver, defaultKeyGenerator)); AnnotatedJCacheableService service = new AnnotatedJCacheableService(cacheManager.getCache("default")); Method m = ReflectionUtils.findMethod(AnnotatedJCacheableService.class, "cache", String.class); try { interceptor.execute(dummyInvoker, service, m, new Object[] {"myId"}); } catch (IllegalStateException ex) { assertTrue(ex.getMessage().contains("JSR-107 only supports a single cache")); } catch (Throwable ex) { fail("Unexpected: " + ex); } }
@Test public void noCacheCouldBeResolved() { JCacheInterceptor interceptor = createInterceptor(createOperationSource( cacheManager, new NamedCacheResolver(cacheManager), // Returns empty list defaultExceptionCacheResolver, defaultKeyGenerator)); AnnotatedJCacheableService service = new AnnotatedJCacheableService(cacheManager.getCache("default")); Method m = ReflectionUtils.findMethod(AnnotatedJCacheableService.class, "cache", String.class); try { interceptor.execute(dummyInvoker, service, m, new Object[] {"myId"}); } catch (IllegalStateException ex) { assertTrue(ex.getMessage().contains("Cache could not have been resolved for")); } catch (Throwable ex) { fail("Unexpected: " + ex); } }
@Override public Object invoke(final MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod(); CacheOperationInvoker aopAllianceInvoker = new CacheOperationInvoker() { @Override public Object invoke() { try { return invocation.proceed(); } catch (Throwable ex) { throw new ThrowableWrapper(ex); } } }; try { return execute(aopAllianceInvoker, invocation.getThis(), method, invocation.getArguments()); } catch (CacheOperationInvoker.ThrowableWrapper th) { throw th.getOriginal(); } }