Search in sources :

Example 1 with RetryException

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

the class StatefulRecoveryRetryTests method testCacheCapacity.

@Test
public void testCacheCapacity() throws Throwable {
    this.retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
    this.retryTemplate.setRetryContextCache(new MapRetryContextCache(1));
    RetryCallback<Object, Exception> callback = context -> {
        StatefulRecoveryRetryTests.this.count++;
        throw new RuntimeException("Barf!");
    };
    try {
        this.retryTemplate.execute(callback, new DefaultRetryState("foo"));
        fail("Expected RuntimeException");
    } catch (RuntimeException e) {
        assertEquals("Barf!", e.getMessage());
    }
    try {
        this.retryTemplate.execute(callback, new DefaultRetryState("bar"));
        fail("Expected RetryException");
    } catch (RetryException e) {
        String message = e.getNestedMessage();
        assertTrue("Message does not contain 'capacity': " + message, message.contains("capacity"));
    }
}
Also used : RetryCallback(cn.taketoday.retry.RetryCallback) Assert.assertNotNull(org.junit.Assert.assertNotNull) RetryPolicy(cn.taketoday.retry.RetryPolicy) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) ArrayList(java.util.ArrayList) RetryState(cn.taketoday.retry.RetryState) RecoveryCallback(cn.taketoday.retry.RecoveryCallback) List(java.util.List) RetryException(cn.taketoday.retry.RetryException) RetryContext(cn.taketoday.retry.RetryContext) Assert.assertFalse(org.junit.Assert.assertFalse) MapRetryContextCache(cn.taketoday.retry.policy.MapRetryContextCache) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) Assert.fail(org.junit.Assert.fail) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) BinaryExceptionClassifier(cn.taketoday.classify.BinaryExceptionClassifier) MapRetryContextCache(cn.taketoday.retry.policy.MapRetryContextCache) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) Test(org.junit.Test)

Example 2 with RetryException

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

the class RetryTemplate method open.

/**
 * Delegate to the {@link RetryPolicy} having checked in the cache for an existing
 * value if the state is not null.
 *
 * @param state a {@link RetryState}
 * @param retryPolicy a {@link RetryPolicy} to delegate the context creation
 * @return a retry context, either a new one or the one used last time the same state
 * was encountered
 */
protected RetryContext open(RetryPolicy retryPolicy, RetryState state) {
    if (state == null) {
        return doOpenInternal(retryPolicy);
    }
    Object key = state.getKey();
    if (state.isForceRefresh()) {
        return doOpenInternal(retryPolicy, state);
    }
    // cache re-hydration.
    if (!retryContextCache.containsKey(key)) {
        // The cache is only used if there is a failure.
        return doOpenInternal(retryPolicy, state);
    }
    RetryContext context = retryContextCache.get(key);
    if (context == null) {
        if (retryContextCache.containsKey(key)) {
            throw new RetryException("Inconsistent state for failed item: no history found. " + "Consider whether equals() or hashCode() for the item might be inconsistent, " + "or if you need to supply a better ItemKeyGenerator");
        }
        // containsKey(), so we have to live with this:
        return doOpenInternal(retryPolicy, state);
    }
    // Start with a clean slate for state that others may be inspecting
    context.removeAttribute(RetryContext.CLOSED);
    context.removeAttribute(RetryContext.EXHAUSTED);
    context.removeAttribute(RetryContext.RECOVERED);
    return context;
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) TerminatedRetryException(cn.taketoday.retry.TerminatedRetryException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException)

Example 3 with RetryException

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

the class StatefulRecoveryRetryTests method testKeyGeneratorNotConsistentAfterFailure.

@Test
public void testKeyGeneratorNotConsistentAfterFailure() throws Throwable {
    RetryPolicy retryPolicy = new SimpleRetryPolicy(3);
    this.retryTemplate.setRetryPolicy(retryPolicy);
    final StringHolder item = new StringHolder("bar");
    RetryState state = new DefaultRetryState(item);
    RetryCallback<StringHolder, Exception> callback = new RetryCallback<StringHolder, Exception>() {

        @Override
        public StringHolder doWithRetry(RetryContext context) throws Exception {
            // This simulates what happens if someone uses a primary key
            // for hashCode and equals and then relies on default key
            // generator
            item.string = item.string + (StatefulRecoveryRetryTests.this.count++);
            throw new RuntimeException("Barf!");
        }
    };
    try {
        this.retryTemplate.execute(callback, state);
        fail("Expected RuntimeException");
    } catch (RuntimeException ex) {
        String message = ex.getMessage();
        assertEquals("Barf!", message);
    }
    // item already...
    try {
        this.retryTemplate.execute(callback, state);
        fail("Expected RetryException");
    } catch (RetryException ex) {
        String message = ex.getNestedMessage();
        assertTrue("Message doesn't contain 'inconsistent': " + message, message.contains("inconsistent"));
    }
    RetryContext context = this.retryTemplate.open(retryPolicy, state);
    // True after exhausted - the history is reset...
    assertEquals(0, context.getRetryCount());
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) RetryPolicy(cn.taketoday.retry.RetryPolicy) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) 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)

Example 4 with RetryException

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

the class StatefulRecoveryRetryTests method testCacheCapacity.

@Test
public void testCacheCapacity() throws Throwable {
    this.retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
    this.retryTemplate.setRetryContextCache(new MapRetryContextCache(1));
    RetryCallback<Object, Exception> callback = context -> {
        StatefulRecoveryRetryTests.this.count++;
        throw new RuntimeException("Barf!");
    };
    try {
        this.retryTemplate.execute(callback, new DefaultRetryState("foo"));
        fail("Expected RuntimeException");
    } catch (RuntimeException e) {
        assertEquals("Barf!", e.getMessage());
    }
    try {
        this.retryTemplate.execute(callback, new DefaultRetryState("bar"));
        fail("Expected RetryException");
    } catch (RetryException e) {
        String message = e.getNestedMessage();
        assertTrue("Message does not contain 'capacity': " + message, message.contains("capacity"));
    }
}
Also used : RetryCallback(cn.taketoday.retry.RetryCallback) Assert.assertNotNull(org.junit.Assert.assertNotNull) RetryPolicy(cn.taketoday.retry.RetryPolicy) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) ArrayList(java.util.ArrayList) RetryState(cn.taketoday.retry.RetryState) RecoveryCallback(cn.taketoday.retry.RecoveryCallback) List(java.util.List) RetryException(cn.taketoday.retry.RetryException) RetryContext(cn.taketoday.retry.RetryContext) Assert.assertFalse(org.junit.Assert.assertFalse) MapRetryContextCache(cn.taketoday.retry.policy.MapRetryContextCache) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) Assert.fail(org.junit.Assert.fail) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) BinaryExceptionClassifier(cn.taketoday.classify.BinaryExceptionClassifier) MapRetryContextCache(cn.taketoday.retry.policy.MapRetryContextCache) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) DataAccessException(cn.taketoday.dao.DataAccessException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException) Test(org.junit.Test)

Example 5 with RetryException

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

the class RetryTemplate method open.

/**
 * Delegate to the {@link RetryPolicy} having checked in the cache for an existing
 * value if the state is not null.
 *
 * @param state a {@link RetryState}
 * @param retryPolicy a {@link RetryPolicy} to delegate the context creation
 * @return a retry context, either a new one or the one used last time the same state
 * was encountered
 */
protected RetryContext open(RetryPolicy retryPolicy, RetryState state) {
    if (state == null) {
        return doOpenInternal(retryPolicy);
    }
    Object key = state.getKey();
    if (state.isForceRefresh()) {
        return doOpenInternal(retryPolicy, state);
    }
    // cache re-hydration.
    if (!retryContextCache.containsKey(key)) {
        // The cache is only used if there is a failure.
        return doOpenInternal(retryPolicy, state);
    }
    RetryContext context = retryContextCache.get(key);
    if (context == null) {
        if (retryContextCache.containsKey(key)) {
            throw new RetryException("Inconsistent state for failed item: no history found. " + "Consider whether equals() or hashCode() for the item might be inconsistent, " + "or if you need to supply a better ItemKeyGenerator");
        }
        // containsKey(), so we have to live with this:
        return doOpenInternal(retryPolicy, state);
    }
    // Start with a clean slate for state that others may be inspecting
    context.removeAttribute(RetryContext.CLOSED);
    context.removeAttribute(RetryContext.EXHAUSTED);
    context.removeAttribute(RetryContext.RECOVERED);
    return context;
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) TerminatedRetryException(cn.taketoday.retry.TerminatedRetryException) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryException(cn.taketoday.retry.RetryException)

Aggregations

ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)6 RetryContext (cn.taketoday.retry.RetryContext)6 RetryException (cn.taketoday.retry.RetryException)6 DataAccessException (cn.taketoday.dao.DataAccessException)4 RetryCallback (cn.taketoday.retry.RetryCallback)4 RetryPolicy (cn.taketoday.retry.RetryPolicy)4 RetryState (cn.taketoday.retry.RetryState)4 NeverRetryPolicy (cn.taketoday.retry.policy.NeverRetryPolicy)4 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)4 Test (org.junit.Test)4 BinaryExceptionClassifier (cn.taketoday.classify.BinaryExceptionClassifier)2 RecoveryCallback (cn.taketoday.retry.RecoveryCallback)2 TerminatedRetryException (cn.taketoday.retry.TerminatedRetryException)2 MapRetryContextCache (cn.taketoday.retry.policy.MapRetryContextCache)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertFalse (org.junit.Assert.assertFalse)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)2