Search in sources :

Example 6 with RetryListener

use of cn.taketoday.retry.RetryListener in project today-framework by TAKETODAY.

the class RetryListenerTests method testCloseInterceptors.

@Test
public void testCloseInterceptors() throws Throwable {
    template.setListeners(new RetryListener[] { new RetryListener() {

        public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable t) {
            count++;
            list.add("1:" + count);
        }
    }, new RetryListener() {

        public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable t) {
            count++;
            list.add("2:" + count);
        }
    } });
    template.execute(new RetryCallback<String, Exception>() {

        public String doWithRetry(RetryContext context) throws Exception {
            return null;
        }
    });
    assertEquals(2, count);
    assertEquals(2, list.size());
    // interceptors are called in reverse order on close...
    assertEquals("2:1", list.get(0));
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) TerminatedRetryException(cn.taketoday.retry.TerminatedRetryException) RetryListener(cn.taketoday.retry.RetryListener) Test(org.junit.Test)

Example 7 with RetryListener

use of cn.taketoday.retry.RetryListener in project today-framework by TAKETODAY.

the class RetryListenerTests method testOnError.

@Test
public void testOnError() throws Throwable {
    template.setRetryPolicy(new NeverRetryPolicy());
    template.setListeners(new RetryListener[] { new RetryListener() {

        public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
            list.add("1");
        }
    }, new RetryListener() {

        public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
            list.add("2");
        }
    } });
    try {
        template.execute(new RetryCallback<String, Exception>() {

            public String doWithRetry(RetryContext context) throws Exception {
                count++;
                throw new IllegalStateException("foo");
            }
        });
        fail("Expected IllegalStateException");
    } catch (IllegalStateException e) {
        assertEquals("foo", e.getMessage());
    }
    // never retry so callback is executed once
    assertEquals(1, count);
    assertEquals(2, list.size());
    // interceptors are called in reverse order on error...
    assertEquals("2", list.get(0));
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) TerminatedRetryException(cn.taketoday.retry.TerminatedRetryException) RetryListener(cn.taketoday.retry.RetryListener) Test(org.junit.Test)

Example 8 with RetryListener

use of cn.taketoday.retry.RetryListener in project today-framework by TAKETODAY.

the class RetryOperationsInterceptorTests method setUp.

@Before
public void setUp() throws Exception {
    this.interceptor = new RetryOperationsInterceptor();
    RetryTemplate retryTemplate = new RetryTemplate();
    final AtomicBoolean calledFirst = new AtomicBoolean();
    retryTemplate.registerListener(new RetryListener() {

        @Override
        public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
            calledFirst.set(true);
            return true;
        }

        @Override
        public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
            RetryOperationsInterceptorTests.this.context = context;
        }
    });
    retryTemplate.registerListener(new RetryListener() {

        @Override
        public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
            assertFalse(calledFirst.get());
            return true;
        }
    }, 0);
    this.interceptor.setRetryOperations(retryTemplate);
    this.target = new ServiceImpl();
    this.service = ProxyFactory.getProxy(Service.class, new SingletonTargetSource(this.target));
    count = 0;
    transactionCount = 0;
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) RetryListener(cn.taketoday.retry.RetryListener) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) Before(org.junit.Before)

Example 9 with RetryListener

use of cn.taketoday.retry.RetryListener in project today-framework by TAKETODAY.

the class StatefulRetryOperationsInterceptorTests method setUp.

@BeforeEach
public void setUp() throws Exception {
    interceptor = new StatefulRetryOperationsInterceptor();
    retryTemplate.registerListener(new RetryListener() {

        @Override
        public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
            StatefulRetryOperationsInterceptorTests.this.context = context;
        }
    });
    interceptor.setRetryOperations(retryTemplate);
    service = ProxyFactory.getProxy(Service.class, new SingletonTargetSource(new ServiceImpl()));
    transformer = ProxyFactory.getProxy(Transformer.class, new SingletonTargetSource(new TransformerImpl()));
    count = 0;
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) RetryListener(cn.taketoday.retry.RetryListener) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 10 with RetryListener

use of cn.taketoday.retry.RetryListener in project today-framework by TAKETODAY.

the class RetryListenerTests method testOpenInterceptors.

@Test
public void testOpenInterceptors() throws Throwable {
    template.setListeners(new RetryListener[] { new RetryListener() {

        public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
            count++;
            list.add("1:" + count);
            return true;
        }
    }, new RetryListener() {

        public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
            count++;
            list.add("2:" + count);
            return true;
        }
    } });
    template.execute(new RetryCallback<String, Exception>() {

        public String doWithRetry(RetryContext context) throws Exception {
            return null;
        }
    });
    assertEquals(2, count);
    assertEquals(2, list.size());
    assertEquals("1:1", list.get(0));
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) TerminatedRetryException(cn.taketoday.retry.TerminatedRetryException) RetryListener(cn.taketoday.retry.RetryListener) Test(org.junit.Test)

Aggregations

RetryListener (cn.taketoday.retry.RetryListener)18 RetryContext (cn.taketoday.retry.RetryContext)14 Test (org.junit.Test)12 TerminatedRetryException (cn.taketoday.retry.TerminatedRetryException)10 SingletonTargetSource (cn.taketoday.aop.target.SingletonTargetSource)4 BinaryExceptionClassifier (cn.taketoday.classify.BinaryExceptionClassifier)4 MaxAttemptsRetryPolicy (cn.taketoday.retry.policy.MaxAttemptsRetryPolicy)4 ExponentialBackOffPolicy (cn.taketoday.retry.backoff.ExponentialBackOffPolicy)2 NoBackOffPolicy (cn.taketoday.retry.backoff.NoBackOffPolicy)2 BinaryExceptionClassifierRetryPolicy (cn.taketoday.retry.policy.BinaryExceptionClassifierRetryPolicy)2 CompositeRetryPolicy (cn.taketoday.retry.policy.CompositeRetryPolicy)2 NeverRetryPolicy (cn.taketoday.retry.policy.NeverRetryPolicy)2 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Before (org.junit.Before)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2