use of cn.taketoday.retry.support.RetryTemplate 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.support.RetryTemplate in project today-infrastructure by TAKETODAY.
the class AnnotationAwareRetryOperationsInterceptor method getStatelessInterceptor.
private MethodInterceptor getStatelessInterceptor(Object target, Method method, Retryable retryable) {
RetryTemplate template = createTemplate(retryable.listeners());
template.setRetryPolicy(getRetryPolicy(retryable));
template.setBackOffPolicy(getBackoffPolicy(retryable.backoff()));
return RetryInterceptorBuilder.stateless().retryOperations(template).label(retryable.label()).recoverer(getRecoverer(target, method)).build();
}
use of cn.taketoday.retry.support.RetryTemplate in project today-infrastructure by TAKETODAY.
the class RetryOperationsInterceptorTests method testDefaultInterceptorWithRetryListenerInspectingTheMethodInvocation.
@Test
public void testDefaultInterceptorWithRetryListenerInspectingTheMethodInvocation() throws Exception {
final String label = "FOO";
final String classTagName = "class";
final String methodTagName = "method";
final String labelTagName = "label";
final Map<String, String> monitoringTags = new HashMap<String, String>();
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(new SimpleRetryPolicy(2));
template.registerListener(new MethodInvocationRetryListenerSupport() {
@Override
protected <T, E extends Throwable> void doClose(RetryContext context, MethodInvocationRetryCallback<T, E> callback, Throwable throwable) {
monitoringTags.put(labelTagName, callback.getLabel());
Method method = callback.getInvocation().getMethod();
monitoringTags.put(classTagName, method.getDeclaringClass().getSimpleName());
monitoringTags.put(methodTagName, method.getName());
}
});
this.interceptor.setLabel(label);
this.interceptor.setRetryOperations(template);
((Advised) this.service).addAdvice(this.interceptor);
this.service.service();
assertEquals(2, count);
assertEquals(3, monitoringTags.entrySet().size());
assertThat(monitoringTags.get(labelTagName), equalTo(label));
assertThat(monitoringTags.get(classTagName), equalTo(Service.class.getSimpleName()));
assertThat(monitoringTags.get(methodTagName), equalTo("service"));
}
use of cn.taketoday.retry.support.RetryTemplate in project today-infrastructure by TAKETODAY.
the class RetryOperationsInterceptorTests method testRetryExceptionAfterTooManyAttempts.
@Test
public void testRetryExceptionAfterTooManyAttempts() throws Exception {
((Advised) this.service).addAdvice(this.interceptor);
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(new NeverRetryPolicy());
this.interceptor.setRetryOperations(template);
try {
this.service.service();
fail("Expected Exception.");
} catch (Exception e) {
assertTrue(e.getMessage().startsWith("Not enough calls"));
}
assertEquals(1, count);
}
use of cn.taketoday.retry.support.RetryTemplate in project today-infrastructure by TAKETODAY.
the class RetryInterceptorBuilderTests method testWithCustomRetryTemplate.
@Test
public void testWithCustomRetryTemplate() {
RetryOperations retryOperations = new RetryTemplate();
StatefulRetryOperationsInterceptor interceptor = RetryInterceptorBuilder.stateful().retryOperations(retryOperations).build();
assertEquals(3, TestUtils.getPropertyValue(interceptor, "retryOperations.retryPolicy.maxAttempts"));
assertSame(retryOperations, TestUtils.getPropertyValue(interceptor, "retryOperations"));
}
Aggregations