Search in sources :

Example 16 with RetryListener

use of cn.taketoday.retry.RetryListener in project today-infrastructure 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 17 with RetryListener

use of cn.taketoday.retry.RetryListener in project today-infrastructure 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)

Example 18 with RetryListener

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

the class RetryTemplateBuilderTests method testBasicCustomization.

@Test
public void testBasicCustomization() {
    RetryListener listener1 = mock(RetryListener.class);
    RetryListener listener2 = mock(RetryListener.class);
    RetryTemplate template = RetryTemplate.builder().maxAttempts(10).exponentialBackoff(99, 1.5, 1717).retryOn(IOException.class).retryOn(Collections.<Class<? extends Throwable>>singletonList(IllegalArgumentException.class)).traversingCauses().withListener(listener1).withListeners(Collections.singletonList(listener2)).build();
    PolicyTuple policyTuple = PolicyTuple.extractWithAsserts(template);
    BinaryExceptionClassifier classifier = policyTuple.exceptionClassifierRetryPolicy.getExceptionClassifier();
    Assert.assertTrue(classifier.classify(new FileNotFoundException()));
    Assert.assertTrue(classifier.classify(new IllegalArgumentException()));
    Assert.assertFalse(classifier.classify(new RuntimeException()));
    Assert.assertFalse(classifier.classify(new OutOfMemoryError()));
    Assert.assertTrue(policyTuple.baseRetryPolicy instanceof MaxAttemptsRetryPolicy);
    Assert.assertEquals(10, ((MaxAttemptsRetryPolicy) policyTuple.baseRetryPolicy).getMaxAttempts());
    List<RetryListener> listeners = Arrays.asList(getPropertyValue(template, "listeners", RetryListener[].class));
    Assert.assertEquals(2, listeners.size());
    Assert.assertTrue(listeners.contains(listener1));
    Assert.assertTrue(listeners.contains(listener2));
    Assert.assertTrue(getPropertyValue(template, "backOffPolicy") instanceof ExponentialBackOffPolicy);
}
Also used : BinaryExceptionClassifier(cn.taketoday.classify.BinaryExceptionClassifier) ExponentialBackOffPolicy(cn.taketoday.retry.backoff.ExponentialBackOffPolicy) MaxAttemptsRetryPolicy(cn.taketoday.retry.policy.MaxAttemptsRetryPolicy) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) 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