CircuitBreakerAspect { @Around("@annotation(circuitBreakerAnnotation)") public Object monitor(final ProceedingJoinPoint pjp, CircuitBreaker circuitBreakerAnnotation) throws Throwable { final String name = circuitBreakerAnnotation.name(); org.fishwife.jrugged.CircuitBreaker circuitBreaker = circuitBreakerFactory.findCircuitBreaker(name); if (circuitBreaker == null) { DefaultFailureInterpreter dfi = new DefaultFailureInterpreter( circuitBreakerAnnotation.ignore(), circuitBreakerAnnotation.limit(), circuitBreakerAnnotation.windowMillis()); CircuitBreakerConfig config = new CircuitBreakerConfig( circuitBreakerAnnotation.resetMillis(), dfi); circuitBreaker = circuitBreakerFactory.createCircuitBreaker(name, config); } if (logger.isDebugEnabled()) { logger.debug("Have @CircuitBreaker method with breaker name {}, " + "wrapping call on method {} of target object {} with status {}", new Object[]{ name, pjp.getSignature().getName(), pjp.getTarget(), circuitBreaker.getStatus()}); } return circuitBreaker.invoke(new Callable<Object>() { public Object call() throws Exception { try { return pjp.proceed(); } catch (Throwable e) { if (e instanceof Exception) { throw (Exception) e; } else if (e instanceof Error) { throw (Error) e; } else { throw new RuntimeException(e); } } } }); } CircuitBreakerAspect(); void setCircuitBreakerFactory(
            CircuitBreakerFactory circuitBreakerFactory); CircuitBreakerFactory getCircuitBreakerFactory(); @Around("@annotation(circuitBreakerAnnotation)") Object monitor(final ProceedingJoinPoint pjp,
              CircuitBreaker circuitBreakerAnnotation);  }
@Test public void testMonitor() throws Throwable { ProceedingJoinPoint mockPjp = createPjpMock(mockSignature, 2); expect(mockPjp.proceed()).andReturn(null).times(2); replay(mockPjp); aspect.monitor(mockPjp, mockAnnotation); aspect.monitor(mockPjp, mockAnnotation); String otherName = "OtherMonitor"; ProceedingJoinPoint otherMockPjp = createPjpMock(mockSignature, 1); expect(otherMockPjp.proceed()).andReturn(null).times(1); replay(otherMockPjp); CircuitBreaker otherMockAnnotation = createMock(CircuitBreaker.class); expect(otherMockAnnotation.name()).andReturn(otherName).anyTimes(); expect(otherMockAnnotation.limit()).andReturn(5).anyTimes(); expect(otherMockAnnotation.resetMillis()).andReturn(30000L).anyTimes(); expect(otherMockAnnotation.windowMillis()).andReturn(10000L).anyTimes(); @SuppressWarnings("unchecked") Class<Throwable>[] ignores = new Class[0]; expect(otherMockAnnotation.ignore()).andReturn(ignores); replay(otherMockAnnotation); aspect.monitor(otherMockPjp, otherMockAnnotation); verifyBreakerExists(TEST_CIRCUIT_BREAKER); verifyBreakerExists(otherName); verify(mockPjp); verify(mockAnnotation); verify(mockSignature); verify(otherMockPjp); verify(otherMockAnnotation); }