Search in sources :

Example 1 with RecoveryCallback

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

the class FatalExceptionRetryPolicyTests method testFatalExceptionWithoutState.

@Test
public void testFatalExceptionWithoutState() throws Throwable {
    MockRetryCallback callback = new MockRetryCallback();
    callback.setExceptionToThrow(new IllegalArgumentException());
    RetryTemplate retryTemplate = new RetryTemplate();
    // Make sure certain exceptions are fatal...
    Map<Class<? extends Throwable>, Boolean> map = new HashMap<Class<? extends Throwable>, Boolean>();
    map.put(IllegalArgumentException.class, false);
    map.put(IllegalStateException.class, false);
    // ... and allow multiple attempts
    SimpleRetryPolicy policy = new SimpleRetryPolicy(3, map);
    retryTemplate.setRetryPolicy(policy);
    RecoveryCallback<String> recoveryCallback = new RecoveryCallback<String>() {

        public String recover(RetryContext context) throws Exception {
            return "bar";
        }
    };
    Object result = null;
    try {
        result = retryTemplate.execute(callback, recoveryCallback);
    } catch (IllegalArgumentException e) {
        // We should swallow the exception when recovery is possible
        fail("Did not expect IllegalArgumentException");
    }
    // Callback is called once: the recovery path should also be called
    assertEquals(1, callback.attempts);
    assertEquals("bar", result);
}
Also used : RecoveryCallback(cn.taketoday.retry.RecoveryCallback) HashMap(java.util.HashMap) RetryContext(cn.taketoday.retry.RetryContext) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) Test(org.junit.Test)

Example 2 with RecoveryCallback

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

the class StatefulRecoveryRetryTests method testCacheCapacityNotReachedIfRecovered.

@Test
public void testCacheCapacityNotReachedIfRecovered() throws Throwable {
    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(1);
    this.retryTemplate.setRetryPolicy(retryPolicy);
    this.retryTemplate.setRetryContextCache(new MapRetryContextCache(2));
    final StringHolder item = new StringHolder("foo");
    RetryState state = new DefaultRetryState(item);
    RetryCallback<Object, Exception> callback = new RetryCallback<Object, Exception>() {

        @Override
        public Object doWithRetry(RetryContext context) throws Exception {
            StatefulRecoveryRetryTests.this.count++;
            throw new RuntimeException("Barf!");
        }
    };
    RecoveryCallback<Object> recoveryCallback = new RecoveryCallback<Object>() {

        @Override
        public Object recover(RetryContext context) throws Exception {
            return null;
        }
    };
    try {
        this.retryTemplate.execute(callback, recoveryCallback, state);
        fail("Expected RuntimeException");
    } catch (RuntimeException e) {
        assertEquals("Barf!", e.getMessage());
    }
    this.retryTemplate.execute(callback, recoveryCallback, state);
    RetryContext context = this.retryTemplate.open(retryPolicy, state);
    // True after exhausted - the history is reset...
    assertEquals(0, context.getRetryCount());
}
Also used : RecoveryCallback(cn.taketoday.retry.RecoveryCallback) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) MapRetryContextCache(cn.taketoday.retry.policy.MapRetryContextCache) RetryState(cn.taketoday.retry.RetryState) RetryCallback(cn.taketoday.retry.RetryCallback) Test(org.junit.Test)

Example 3 with RecoveryCallback

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

the class StatefulRecoveryRetryTests method testSwitchToStatelessForNoRollback.

@Test
public void testSwitchToStatelessForNoRollback() throws Throwable {
    this.retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
    // Roll back for these:
    BinaryExceptionClassifier classifier = new BinaryExceptionClassifier(Collections.<Class<? extends Throwable>>singleton(DataAccessException.class));
    // ...but not these:
    assertFalse(classifier.classify(new RuntimeException()));
    final String input = "foo";
    RetryState state = new DefaultRetryState(input, classifier);
    RetryCallback<String, Exception> callback = new RetryCallback<String, Exception>() {

        @Override
        public String doWithRetry(RetryContext context) throws Exception {
            throw new RuntimeException("Barf!");
        }
    };
    RecoveryCallback<String> recoveryCallback = new RecoveryCallback<String>() {

        @Override
        public String recover(RetryContext context) {
            StatefulRecoveryRetryTests.this.count++;
            StatefulRecoveryRetryTests.this.list.add(input);
            return input;
        }
    };
    Object result = null;
    // On the second retry, the recovery path is taken...
    result = this.retryTemplate.execute(callback, recoveryCallback, state);
    // default result is the item
    assertEquals(input, result);
    assertEquals(1, this.count);
    assertEquals(input, this.list.get(0));
}
Also used : BinaryExceptionClassifier(cn.taketoday.classify.BinaryExceptionClassifier) RecoveryCallback(cn.taketoday.retry.RecoveryCallback) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) DataAccessException(cn.taketoday.dao.DataAccessException) RetryState(cn.taketoday.retry.RetryState) RetryCallback(cn.taketoday.retry.RetryCallback) Test(org.junit.Test)

Example 4 with RecoveryCallback

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

the class StatefulRecoveryRetryTests method testSwitchToStatelessForNoRollback.

@Test
public void testSwitchToStatelessForNoRollback() throws Throwable {
    this.retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
    // Roll back for these:
    BinaryExceptionClassifier classifier = new BinaryExceptionClassifier(Collections.<Class<? extends Throwable>>singleton(DataAccessException.class));
    // ...but not these:
    assertFalse(classifier.classify(new RuntimeException()));
    final String input = "foo";
    RetryState state = new DefaultRetryState(input, classifier);
    RetryCallback<String, Exception> callback = new RetryCallback<String, Exception>() {

        @Override
        public String doWithRetry(RetryContext context) throws Exception {
            throw new RuntimeException("Barf!");
        }
    };
    RecoveryCallback<String> recoveryCallback = new RecoveryCallback<String>() {

        @Override
        public String recover(RetryContext context) {
            StatefulRecoveryRetryTests.this.count++;
            StatefulRecoveryRetryTests.this.list.add(input);
            return input;
        }
    };
    Object result = null;
    // On the second retry, the recovery path is taken...
    result = this.retryTemplate.execute(callback, recoveryCallback, state);
    // default result is the item
    assertEquals(input, result);
    assertEquals(1, this.count);
    assertEquals(input, this.list.get(0));
}
Also used : BinaryExceptionClassifier(cn.taketoday.classify.BinaryExceptionClassifier) RecoveryCallback(cn.taketoday.retry.RecoveryCallback) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) DataAccessException(cn.taketoday.dao.DataAccessException) RetryState(cn.taketoday.retry.RetryState) RetryCallback(cn.taketoday.retry.RetryCallback) Test(org.junit.Test)

Example 5 with RecoveryCallback

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

the class StatefulRecoveryRetryTests method testRecover.

@Test
public void testRecover() throws Throwable {
    this.retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
    final String input = "foo";
    RetryState state = new DefaultRetryState(input);
    RetryCallback<String, Exception> callback = new RetryCallback<String, Exception>() {

        @Override
        public String doWithRetry(RetryContext context) throws Exception {
            throw new RuntimeException("Barf!");
        }
    };
    RecoveryCallback<String> recoveryCallback = new RecoveryCallback<String>() {

        @Override
        public String recover(RetryContext context) {
            StatefulRecoveryRetryTests.this.count++;
            StatefulRecoveryRetryTests.this.list.add(input);
            return input;
        }
    };
    Object result = null;
    try {
        result = this.retryTemplate.execute(callback, recoveryCallback, state);
        fail("Expected exception on first try");
    } catch (Exception e) {
    // expected...
    }
    // On the second retry, the recovery path is taken...
    result = this.retryTemplate.execute(callback, recoveryCallback, state);
    // default result is the item
    assertEquals(input, result);
    assertEquals(1, this.count);
    assertEquals(input, this.list.get(0));
}
Also used : RecoveryCallback(cn.taketoday.retry.RecoveryCallback) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) RetryState(cn.taketoday.retry.RetryState) RetryCallback(cn.taketoday.retry.RetryCallback) Test(org.junit.Test)

Aggregations

RecoveryCallback (cn.taketoday.retry.RecoveryCallback)10 RetryContext (cn.taketoday.retry.RetryContext)10 Test (org.junit.Test)10 DataAccessException (cn.taketoday.dao.DataAccessException)6 ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)6 RetryCallback (cn.taketoday.retry.RetryCallback)6 RetryException (cn.taketoday.retry.RetryException)6 RetryState (cn.taketoday.retry.RetryState)6 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)6 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)4 HashMap (java.util.HashMap)4 BinaryExceptionClassifier (cn.taketoday.classify.BinaryExceptionClassifier)2 MapRetryContextCache (cn.taketoday.retry.policy.MapRetryContextCache)2 DefaultRetryState (cn.taketoday.retry.support.DefaultRetryState)2