use of cn.taketoday.retry.policy.CircuitBreakerRetryPolicy in project today-infrastructure by TAKETODAY.
the class AnnotationAwareRetryOperationsInterceptor method getStatefulInterceptor.
private MethodInterceptor getStatefulInterceptor(Object target, Method method, Retryable retryable) {
RetryTemplate template = createTemplate(retryable.listeners());
template.setRetryContextCache(this.retryContextCache);
CircuitBreaker circuit = AnnotatedElementUtils.findMergedAnnotation(method, CircuitBreaker.class);
if (circuit == null) {
circuit = findAnnotationOnTarget(target, method, CircuitBreaker.class);
}
if (circuit != null) {
RetryPolicy policy = getRetryPolicy(circuit);
CircuitBreakerRetryPolicy breaker = new CircuitBreakerRetryPolicy(policy);
breaker.setOpenTimeout(getOpenTimeout(circuit));
breaker.setResetTimeout(getResetTimeout(circuit));
template.setRetryPolicy(breaker);
template.setBackOffPolicy(new NoBackOffPolicy());
String label = circuit.label();
if (!StringUtils.hasText(label)) {
label = method.toGenericString();
}
return RetryInterceptorBuilder.circuitBreaker().keyGenerator(new FixedKeyGenerator("circuit")).retryOperations(template).recoverer(getRecoverer(target, method)).label(label).build();
}
RetryPolicy policy = getRetryPolicy(retryable);
template.setRetryPolicy(policy);
template.setBackOffPolicy(getBackoffPolicy(retryable.backoff()));
String label = retryable.label();
return RetryInterceptorBuilder.stateful().keyGenerator(this.methodArgumentsKeyGenerator).newMethodArgumentsIdentifier(this.newMethodArgumentsIdentifier).retryOperations(template).label(label).recoverer(getRecoverer(target, method)).build();
}
use of cn.taketoday.retry.policy.CircuitBreakerRetryPolicy in project today-framework by TAKETODAY.
the class CircuitBreakerStatisticsTests method testFailedRecoveryCountsAsAbort.
@Test
public void testFailedRecoveryCountsAsAbort() throws Throwable {
this.retryTemplate.setRetryPolicy(new CircuitBreakerRetryPolicy(new NeverRetryPolicy()));
this.recovery = new RecoveryCallback<Object>() {
@Override
public Object recover(RetryContext context) throws Exception {
throw new ExhaustedRetryException("Planned exhausted");
}
};
try {
this.retryTemplate.execute(this.callback, this.recovery, this.state);
fail("Expected ExhaustedRetryException");
} catch (ExhaustedRetryException e) {
// Fine
}
MutableRetryStatistics stats = (MutableRetryStatistics) repository.findOne("test");
assertEquals(1, stats.getStartedCount());
assertEquals(1, stats.getAbortCount());
assertEquals(0, stats.getRecoveryCount());
}
use of cn.taketoday.retry.policy.CircuitBreakerRetryPolicy in project today-framework by TAKETODAY.
the class CircuitBreakerStatisticsTests method testCircuitOpenWhenNotRetryable.
@Test
public void testCircuitOpenWhenNotRetryable() throws Throwable {
this.retryTemplate.setRetryPolicy(new CircuitBreakerRetryPolicy(new NeverRetryPolicy()));
Object result = this.retryTemplate.execute(this.callback, this.recovery, this.state);
MutableRetryStatistics stats = (MutableRetryStatistics) repository.findOne("test");
assertEquals(1, stats.getStartedCount());
assertEquals(RECOVERED, result);
result = this.retryTemplate.execute(this.callback, this.recovery, this.state);
assertEquals(RECOVERED, result);
assertEquals("There should be two recoveries", 2, stats.getRecoveryCount());
assertEquals("There should only be one error because the circuit is now open", 1, stats.getErrorCount());
assertEquals(true, stats.getAttribute(CircuitBreakerRetryPolicy.CIRCUIT_OPEN));
// Both recoveries are through a short circuit because we used NeverRetryPolicy
assertEquals(2, stats.getAttribute(CircuitBreakerRetryPolicy.CIRCUIT_SHORT_COUNT));
resetAndAssert(this.cache, stats);
}
use of cn.taketoday.retry.policy.CircuitBreakerRetryPolicy in project today-framework by TAKETODAY.
the class CircuitBreakerStatisticsTests method testCircuitOpenWithNoRecovery.
@Test
public void testCircuitOpenWithNoRecovery() throws Throwable {
this.retryTemplate.setRetryPolicy(new CircuitBreakerRetryPolicy(new NeverRetryPolicy()));
this.retryTemplate.setThrowLastExceptionOnExhausted(true);
try {
this.retryTemplate.execute(this.callback, this.state);
} catch (Exception e) {
}
try {
this.retryTemplate.execute(this.callback, this.state);
} catch (Exception e) {
}
MutableRetryStatistics stats = (MutableRetryStatistics) repository.findOne("test");
assertEquals("There should be two aborts", 2, stats.getAbortCount());
assertEquals("There should only be one error because the circuit is now open", 1, stats.getErrorCount());
assertEquals(true, stats.getAttribute(CircuitBreakerRetryPolicy.CIRCUIT_OPEN));
resetAndAssert(this.cache, stats);
}
use of cn.taketoday.retry.policy.CircuitBreakerRetryPolicy in project today-framework by TAKETODAY.
the class AnnotationAwareRetryOperationsInterceptor method getStatefulInterceptor.
private MethodInterceptor getStatefulInterceptor(Object target, Method method, Retryable retryable) {
RetryTemplate template = createTemplate(retryable.listeners());
template.setRetryContextCache(this.retryContextCache);
CircuitBreaker circuit = AnnotatedElementUtils.findMergedAnnotation(method, CircuitBreaker.class);
if (circuit == null) {
circuit = findAnnotationOnTarget(target, method, CircuitBreaker.class);
}
if (circuit != null) {
RetryPolicy policy = getRetryPolicy(circuit);
CircuitBreakerRetryPolicy breaker = new CircuitBreakerRetryPolicy(policy);
breaker.setOpenTimeout(getOpenTimeout(circuit));
breaker.setResetTimeout(getResetTimeout(circuit));
template.setRetryPolicy(breaker);
template.setBackOffPolicy(new NoBackOffPolicy());
String label = circuit.label();
if (!StringUtils.hasText(label)) {
label = method.toGenericString();
}
return RetryInterceptorBuilder.circuitBreaker().keyGenerator(new FixedKeyGenerator("circuit")).retryOperations(template).recoverer(getRecoverer(target, method)).label(label).build();
}
RetryPolicy policy = getRetryPolicy(retryable);
template.setRetryPolicy(policy);
template.setBackOffPolicy(getBackoffPolicy(retryable.backoff()));
String label = retryable.label();
return RetryInterceptorBuilder.stateful().keyGenerator(this.methodArgumentsKeyGenerator).newMethodArgumentsIdentifier(this.newMethodArgumentsIdentifier).retryOperations(template).label(label).recoverer(getRecoverer(target, method)).build();
}
Aggregations