Search in sources :

Example 1 with BinaryExceptionClassifier

use of cn.taketoday.classify.BinaryExceptionClassifier in project today-infrastructure by TAKETODAY.

the class CircuitBreakerStatisticsTests method init.

@Before
public void init() {
    this.callback = new MockRetryCallback();
    this.recovery = new RecoveryCallback<Object>() {

        @Override
        public Object recover(RetryContext context) throws Exception {
            return RECOVERED;
        }
    };
    this.retryTemplate = new RetryTemplate();
    this.cache = new MapRetryContextCache();
    this.retryTemplate.setRetryContextCache(this.cache);
    retryTemplate.setListeners(new RetryListener[] { listener });
    this.callback.setAttemptsBeforeSuccess(1);
    // No rollback by default (so exceptions are not rethrown)
    this.state = new DefaultRetryState("retry", new BinaryExceptionClassifier(false));
}
Also used : MapRetryContextCache(cn.taketoday.retry.policy.MapRetryContextCache) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) BinaryExceptionClassifier(cn.taketoday.classify.BinaryExceptionClassifier) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) RetryContext(cn.taketoday.retry.RetryContext) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Before(org.junit.Before)

Example 2 with BinaryExceptionClassifier

use of cn.taketoday.classify.BinaryExceptionClassifier in project today-infrastructure by TAKETODAY.

the class RetryTemplateBuilder method build.

/* ---------------- Building -------------- */
/**
 * Finish configuration and build resulting {@link RetryTemplate}. For default
 * behaviour and concurrency note see class-level doc of {@link RetryTemplateBuilder}.
 * The {@code retryPolicy} of the returned {@link RetryTemplate} is always an instance
 * of {@link CompositeRetryPolicy}, that consists of one base policy, and of
 * {@link BinaryExceptionClassifierRetryPolicy}. The motivation is: whatever base
 * policy we use, exception classification is extremely recommended.
 *
 * @return new instance of {@link RetryTemplate}
 */
public RetryTemplate build() {
    RetryTemplate retryTemplate = new RetryTemplate();
    // Exception classifier
    BinaryExceptionClassifier exceptionClassifier = classifierBuilder != null ? classifierBuilder.build() : BinaryExceptionClassifier.defaultClassifier();
    if (this.baseRetryPolicy == null) {
        this.baseRetryPolicy = new MaxAttemptsRetryPolicy();
    }
    CompositeRetryPolicy finalPolicy = new CompositeRetryPolicy();
    finalPolicy.setPolicies(new RetryPolicy[] { baseRetryPolicy, new BinaryExceptionClassifierRetryPolicy(exceptionClassifier) });
    retryTemplate.setRetryPolicy(finalPolicy);
    if (this.backOffPolicy == null) {
        this.backOffPolicy = new NoBackOffPolicy();
    }
    retryTemplate.setBackOffPolicy(this.backOffPolicy);
    if (this.listeners != null) {
        retryTemplate.setListeners(this.listeners.toArray(new RetryListener[0]));
    }
    return retryTemplate;
}
Also used : BinaryExceptionClassifier(cn.taketoday.classify.BinaryExceptionClassifier) MaxAttemptsRetryPolicy(cn.taketoday.retry.policy.MaxAttemptsRetryPolicy) BinaryExceptionClassifierRetryPolicy(cn.taketoday.retry.policy.BinaryExceptionClassifierRetryPolicy) NoBackOffPolicy(cn.taketoday.retry.backoff.NoBackOffPolicy) CompositeRetryPolicy(cn.taketoday.retry.policy.CompositeRetryPolicy) RetryListener(cn.taketoday.retry.RetryListener)

Example 3 with BinaryExceptionClassifier

use of cn.taketoday.classify.BinaryExceptionClassifier 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 BinaryExceptionClassifier

use of cn.taketoday.classify.BinaryExceptionClassifier in project today-framework by TAKETODAY.

the class RetryTemplateBuilder method build.

/* ---------------- Building -------------- */
/**
 * Finish configuration and build resulting {@link RetryTemplate}. For default
 * behaviour and concurrency note see class-level doc of {@link RetryTemplateBuilder}.
 * The {@code retryPolicy} of the returned {@link RetryTemplate} is always an instance
 * of {@link CompositeRetryPolicy}, that consists of one base policy, and of
 * {@link BinaryExceptionClassifierRetryPolicy}. The motivation is: whatever base
 * policy we use, exception classification is extremely recommended.
 *
 * @return new instance of {@link RetryTemplate}
 */
public RetryTemplate build() {
    RetryTemplate retryTemplate = new RetryTemplate();
    // Exception classifier
    BinaryExceptionClassifier exceptionClassifier = classifierBuilder != null ? classifierBuilder.build() : BinaryExceptionClassifier.defaultClassifier();
    if (this.baseRetryPolicy == null) {
        this.baseRetryPolicy = new MaxAttemptsRetryPolicy();
    }
    CompositeRetryPolicy finalPolicy = new CompositeRetryPolicy();
    finalPolicy.setPolicies(new RetryPolicy[] { baseRetryPolicy, new BinaryExceptionClassifierRetryPolicy(exceptionClassifier) });
    retryTemplate.setRetryPolicy(finalPolicy);
    if (this.backOffPolicy == null) {
        this.backOffPolicy = new NoBackOffPolicy();
    }
    retryTemplate.setBackOffPolicy(this.backOffPolicy);
    if (this.listeners != null) {
        retryTemplate.setListeners(this.listeners.toArray(new RetryListener[0]));
    }
    return retryTemplate;
}
Also used : BinaryExceptionClassifier(cn.taketoday.classify.BinaryExceptionClassifier) MaxAttemptsRetryPolicy(cn.taketoday.retry.policy.MaxAttemptsRetryPolicy) BinaryExceptionClassifierRetryPolicy(cn.taketoday.retry.policy.BinaryExceptionClassifierRetryPolicy) NoBackOffPolicy(cn.taketoday.retry.backoff.NoBackOffPolicy) CompositeRetryPolicy(cn.taketoday.retry.policy.CompositeRetryPolicy) RetryListener(cn.taketoday.retry.RetryListener)

Example 5 with BinaryExceptionClassifier

use of cn.taketoday.classify.BinaryExceptionClassifier in project today-framework 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

BinaryExceptionClassifier (cn.taketoday.classify.BinaryExceptionClassifier)14 RetryContext (cn.taketoday.retry.RetryContext)6 Test (org.junit.Test)6 ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)4 RetryListener (cn.taketoday.retry.RetryListener)4 MaxAttemptsRetryPolicy (cn.taketoday.retry.policy.MaxAttemptsRetryPolicy)4 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)4 DefaultRetryState (cn.taketoday.retry.support.DefaultRetryState)4 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)4 FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)4 Before (org.junit.Before)4 DataAccessException (cn.taketoday.dao.DataAccessException)2 RecoveryCallback (cn.taketoday.retry.RecoveryCallback)2 RetryCallback (cn.taketoday.retry.RetryCallback)2 RetryException (cn.taketoday.retry.RetryException)2 RetryState (cn.taketoday.retry.RetryState)2 ExponentialBackOffPolicy (cn.taketoday.retry.backoff.ExponentialBackOffPolicy)2 NoBackOffPolicy (cn.taketoday.retry.backoff.NoBackOffPolicy)2 BinaryExceptionClassifierRetryPolicy (cn.taketoday.retry.policy.BinaryExceptionClassifierRetryPolicy)2