Search in sources :

Example 1 with MethodInvocationRetryListenerSupport

use of cn.taketoday.retry.listener.MethodInvocationRetryListenerSupport 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 2 with MethodInvocationRetryListenerSupport

use of cn.taketoday.retry.listener.MethodInvocationRetryListenerSupport in project today-framework 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)

Aggregations

Advised (cn.taketoday.aop.framework.Advised)2 RetryContext (cn.taketoday.retry.RetryContext)2 MethodInvocationRetryListenerSupport (cn.taketoday.retry.listener.MethodInvocationRetryListenerSupport)2 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)2 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2