Search in sources :

Example 1 with RetryTemplate

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();
}
Also used : FixedKeyGenerator(cn.taketoday.retry.interceptor.FixedKeyGenerator) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) NoBackOffPolicy(cn.taketoday.retry.backoff.NoBackOffPolicy) CircuitBreakerRetryPolicy(cn.taketoday.retry.policy.CircuitBreakerRetryPolicy) RetryPolicy(cn.taketoday.retry.RetryPolicy) ExpressionRetryPolicy(cn.taketoday.retry.policy.ExpressionRetryPolicy) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) CircuitBreakerRetryPolicy(cn.taketoday.retry.policy.CircuitBreakerRetryPolicy)

Example 2 with RetryTemplate

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();
}
Also used : RetryTemplate(cn.taketoday.retry.support.RetryTemplate)

Example 3 with RetryTemplate

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"));
}
Also used : MethodInvocationRetryListenerSupport(cn.taketoday.retry.listener.MethodInvocationRetryListenerSupport) HashMap(java.util.HashMap) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) Method(java.lang.reflect.Method) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) Advised(cn.taketoday.aop.framework.Advised) Test(org.junit.Test)

Example 4 with RetryTemplate

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);
}
Also used : RetryTemplate(cn.taketoday.retry.support.RetryTemplate) Advised(cn.taketoday.aop.framework.Advised) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) Test(org.junit.Test)

Example 5 with RetryTemplate

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"));
}
Also used : RetryTemplate(cn.taketoday.retry.support.RetryTemplate) RetryOperations(cn.taketoday.retry.RetryOperations) Test(org.junit.Test)

Aggregations

RetryTemplate (cn.taketoday.retry.support.RetryTemplate)52 Test (org.junit.Test)38 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)26 DefaultRetryState (cn.taketoday.retry.support.DefaultRetryState)26 RetryContext (cn.taketoday.retry.RetryContext)22 RetryState (cn.taketoday.retry.RetryState)16 Advised (cn.taketoday.aop.framework.Advised)12 RetryStatistics (cn.taketoday.retry.RetryStatistics)12 ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)10 RecoveryCallback (cn.taketoday.retry.RecoveryCallback)8 ArrayList (java.util.ArrayList)8 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)8 SingletonTargetSource (cn.taketoday.aop.target.SingletonTargetSource)6 RetryListener (cn.taketoday.retry.RetryListener)6 RetryOperations (cn.taketoday.retry.RetryOperations)6 NeverRetryPolicy (cn.taketoday.retry.policy.NeverRetryPolicy)6 HashMap (java.util.HashMap)6 MethodInvocation (org.aopalliance.intercept.MethodInvocation)6 Before (org.junit.Before)5 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)4