Search in sources :

Example 16 with RetryPolicy

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

the class ExceptionClassifierRetryPolicy method close.

/**
 * Delegate to the policy currently activated in the context.
 *
 * @see RetryPolicy#close(RetryContext)
 */
public void close(RetryContext context) {
    RetryPolicy policy = (RetryPolicy) context;
    policy.close(context);
}
Also used : RetryPolicy(cn.taketoday.retry.RetryPolicy)

Example 17 with RetryPolicy

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

the class ExceptionClassifierRetryPolicyTests method testClassifierOperates.

@SuppressWarnings("serial")
@Test
public void testClassifierOperates() throws Exception {
    RetryContext context = policy.open(null);
    assertNotNull(context);
    assertTrue(policy.canRetry(context));
    policy.registerThrowable(context, new IllegalArgumentException());
    // NeverRetryPolicy is the
    assertFalse(policy.canRetry(context));
    // default
    policy.setExceptionClassifier(new Classifier<Throwable, RetryPolicy>() {

        public RetryPolicy classify(Throwable throwable) {
            if (throwable != null) {
                return new AlwaysRetryPolicy();
            }
            return new NeverRetryPolicy();
        }
    });
    // The context saves the classifier, so changing it now has no effect
    assertFalse(policy.canRetry(context));
    policy.registerThrowable(context, new IllegalArgumentException());
    assertFalse(policy.canRetry(context));
    // But now the classifier will be active in the new context...
    context = policy.open(null);
    assertTrue(policy.canRetry(context));
    policy.registerThrowable(context, new IllegalArgumentException());
    assertTrue(policy.canRetry(context));
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) RetryPolicy(cn.taketoday.retry.RetryPolicy) Test(org.junit.Test)

Example 18 with RetryPolicy

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

the class ExceptionClassifierRetryPolicyTests method testClose.

@SuppressWarnings("serial")
@Test
public void testClose() throws Exception {
    policy.setExceptionClassifier(new Classifier<Throwable, RetryPolicy>() {

        public RetryPolicy classify(Throwable throwable) {
            return new MockRetryPolicySupport() {

                public void close(RetryContext context) {
                    count++;
                }
            };
        }
    });
    RetryContext context = policy.open(null);
    // The mapped (child) policy hasn't been used yet, so if we close now
    // we don't incur the possible expense of creating the child context.
    policy.close(context);
    // not classified yet
    assertEquals(0, count);
    // This forces a child context to be created and the child policy is
    // then closed
    policy.registerThrowable(context, new IllegalStateException());
    policy.close(context);
    // now classified
    assertEquals(1, count);
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) RetryPolicy(cn.taketoday.retry.RetryPolicy) Test(org.junit.Test)

Example 19 with RetryPolicy

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

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

the class StatefulRecoveryRetryTests method testExhaustedClearsHistoryAfterLastAttempt.

@Test
public void testExhaustedClearsHistoryAfterLastAttempt() throws Throwable {
    RetryPolicy retryPolicy = new SimpleRetryPolicy(1);
    this.retryTemplate.setRetryPolicy(retryPolicy);
    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!");
        }
    };
    try {
        this.retryTemplate.execute(callback, state);
        fail("Expected ExhaustedRetryException");
    } catch (RuntimeException e) {
        assertEquals("Barf!", e.getMessage());
    }
    try {
        this.retryTemplate.execute(callback, state);
        fail("Expected ExhaustedRetryException");
    } catch (ExhaustedRetryException e) {
    // expected
    }
    RetryContext context = this.retryTemplate.open(retryPolicy, state);
    // True after exhausted - the history is reset...
    assertTrue(retryPolicy.canRetry(context));
}
Also used : ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) 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)

Aggregations

RetryPolicy (cn.taketoday.retry.RetryPolicy)20 RetryContext (cn.taketoday.retry.RetryContext)12 Test (org.junit.Test)12 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)8 ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)6 RetryException (cn.taketoday.retry.RetryException)6 DataAccessException (cn.taketoday.dao.DataAccessException)4 RetryCallback (cn.taketoday.retry.RetryCallback)4 RetryState (cn.taketoday.retry.RetryState)4 NoBackOffPolicy (cn.taketoday.retry.backoff.NoBackOffPolicy)4 NeverRetryPolicy (cn.taketoday.retry.policy.NeverRetryPolicy)4 TerminatedRetryException (cn.taketoday.retry.TerminatedRetryException)2 BackOffContext (cn.taketoday.retry.backoff.BackOffContext)2 BackOffInterruptedException (cn.taketoday.retry.backoff.BackOffInterruptedException)2 BackOffPolicy (cn.taketoday.retry.backoff.BackOffPolicy)2 RetryContextSupport (cn.taketoday.retry.context.RetryContextSupport)2 FixedKeyGenerator (cn.taketoday.retry.interceptor.FixedKeyGenerator)2 AlwaysRetryPolicy (cn.taketoday.retry.policy.AlwaysRetryPolicy)2 BinaryExceptionClassifierRetryPolicy (cn.taketoday.retry.policy.BinaryExceptionClassifierRetryPolicy)2 CircuitBreakerRetryPolicy (cn.taketoday.retry.policy.CircuitBreakerRetryPolicy)2