Search in sources :

Example 11 with RetryPolicy

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

the class ExceptionClassifierRetryPolicy method registerThrowable.

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

Example 12 with RetryPolicy

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

Example 13 with RetryPolicy

use of cn.taketoday.retry.RetryPolicy 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 14 with RetryPolicy

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

the class RetryTemplateBuilderTests method testCustomPolicy.

@Test
public void testCustomPolicy() {
    RetryPolicy customPolicy = mock(RetryPolicy.class);
    RetryTemplate template = RetryTemplate.builder().customPolicy(customPolicy).build();
    PolicyTuple policyTuple = PolicyTuple.extractWithAsserts(template);
    assertDefaultClassifier(policyTuple);
    Assert.assertEquals(customPolicy, policyTuple.baseRetryPolicy);
}
Also used : AlwaysRetryPolicy(cn.taketoday.retry.policy.AlwaysRetryPolicy) RetryPolicy(cn.taketoday.retry.RetryPolicy) MaxAttemptsRetryPolicy(cn.taketoday.retry.policy.MaxAttemptsRetryPolicy) TimeoutRetryPolicy(cn.taketoday.retry.policy.TimeoutRetryPolicy) CompositeRetryPolicy(cn.taketoday.retry.policy.CompositeRetryPolicy) BinaryExceptionClassifierRetryPolicy(cn.taketoday.retry.policy.BinaryExceptionClassifierRetryPolicy) Test(org.junit.Test)

Example 15 with RetryPolicy

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

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