Search in sources :

Example 1 with TerminatedRetryException

use of org.springframework.retry.TerminatedRetryException in project spring-retry by spring-projects.

the class RetryTemplateTests method testFailedPolicy.

@SuppressWarnings("serial")
@Test
public void testFailedPolicy() throws Throwable {
    RetryTemplate retryTemplate = new RetryTemplate();
    retryTemplate.setRetryPolicy(new NeverRetryPolicy() {

        @Override
        public void registerThrowable(RetryContext context, Throwable throwable) {
            throw new RuntimeException("Planned");
        }
    });
    try {
        retryTemplate.execute(new RetryCallback<Object, Exception>() {

            @Override
            public Object doWithRetry(RetryContext context) throws Exception {
                throw new RuntimeException("Realllly bad!");
            }
        });
        fail("Expected Error");
    } catch (TerminatedRetryException e) {
        assertEquals("Planned", e.getCause().getMessage());
    }
}
Also used : RetryContext(org.springframework.retry.RetryContext) NeverRetryPolicy(org.springframework.retry.policy.NeverRetryPolicy) TerminatedRetryException(org.springframework.retry.TerminatedRetryException) BackOffInterruptedException(org.springframework.retry.backoff.BackOffInterruptedException) TerminatedRetryException(org.springframework.retry.TerminatedRetryException) Test(org.junit.Test)

Example 2 with TerminatedRetryException

use of org.springframework.retry.TerminatedRetryException in project spring-retry by spring-projects.

the class RetryTemplate method doExecute.

/**
 * Execute the callback once if the policy dictates that we can, otherwise execute the
 * recovery callback.
 * @param recoveryCallback the {@link RecoveryCallback}
 * @param retryCallback the {@link RetryCallback}
 * @param state the {@link RetryState}
 * @param <T> the type of the return value
 * @param <E> the exception type to throw
 * @see RetryOperations#execute(RetryCallback, RecoveryCallback, RetryState)
 * @throws ExhaustedRetryException if the retry has been exhausted.
 * @throws E an exception if the retry operation fails
 * @return T the retried value
 */
protected <T, E extends Throwable> T doExecute(RetryCallback<T, E> retryCallback, RecoveryCallback<T> recoveryCallback, RetryState state) throws E, ExhaustedRetryException {
    RetryPolicy retryPolicy = this.retryPolicy;
    BackOffPolicy backOffPolicy = this.backOffPolicy;
    // Allow the retry policy to initialise itself...
    RetryContext context = open(retryPolicy, state);
    if (this.logger.isTraceEnabled()) {
        this.logger.trace("RetryContext retrieved: " + context);
    }
    // Make sure the context is available globally for clients who need
    // it...
    RetrySynchronizationManager.register(context);
    Throwable lastException = null;
    boolean exhausted = false;
    try {
        // Give clients a chance to enhance the context...
        boolean running = doOpenInterceptors(retryCallback, context);
        if (!running) {
            throw new TerminatedRetryException("Retry terminated abnormally by interceptor before first attempt");
        }
        // Get or Start the backoff context...
        BackOffContext backOffContext = null;
        Object resource = context.getAttribute("backOffContext");
        if (resource instanceof BackOffContext) {
            backOffContext = (BackOffContext) resource;
        }
        if (backOffContext == null) {
            backOffContext = backOffPolicy.start(context);
            if (backOffContext != null) {
                context.setAttribute("backOffContext", backOffContext);
            }
        }
        /*
			 * We allow the whole loop to be skipped if the policy or context already
			 * forbid the first try. This is used in the case of external retry to allow a
			 * recovery in handleRetryExhausted without the callback processing (which
			 * would throw an exception).
			 */
        while (canRetry(retryPolicy, context) && !context.isExhaustedOnly()) {
            try {
                if (this.logger.isDebugEnabled()) {
                    this.logger.debug("Retry: count=" + context.getRetryCount());
                }
                // Reset the last exception, so if we are successful
                // the close interceptors will not think we failed...
                lastException = null;
                return retryCallback.doWithRetry(context);
            } catch (Throwable e) {
                lastException = e;
                try {
                    registerThrowable(retryPolicy, state, context, e);
                } catch (Exception ex) {
                    throw new TerminatedRetryException("Could not register throwable", ex);
                } finally {
                    doOnErrorInterceptors(retryCallback, context, e);
                }
                if (canRetry(retryPolicy, context) && !context.isExhaustedOnly()) {
                    try {
                        backOffPolicy.backOff(backOffContext);
                    } catch (BackOffInterruptedException ex) {
                        lastException = e;
                        // back off was prevented by another thread - fail the retry
                        if (this.logger.isDebugEnabled()) {
                            this.logger.debug("Abort retry because interrupted: count=" + context.getRetryCount());
                        }
                        throw ex;
                    }
                }
                if (this.logger.isDebugEnabled()) {
                    this.logger.debug("Checking for rethrow: count=" + context.getRetryCount());
                }
                if (shouldRethrow(retryPolicy, context, state)) {
                    if (this.logger.isDebugEnabled()) {
                        this.logger.debug("Rethrow in retry for policy: count=" + context.getRetryCount());
                    }
                    throw RetryTemplate.<E>wrapIfNecessary(e);
                }
            }
            /*
				 * A stateful attempt that can retry may rethrow the exception before now,
				 * but if we get this far in a stateful retry there's a reason for it,
				 * like a circuit breaker or a rollback classifier.
				 */
            if (state != null && context.hasAttribute(GLOBAL_STATE)) {
                break;
            }
        }
        if (state == null && this.logger.isDebugEnabled()) {
            this.logger.debug("Retry failed last attempt: count=" + context.getRetryCount());
        }
        exhausted = true;
        return handleRetryExhausted(recoveryCallback, context, state);
    } catch (Throwable e) {
        throw RetryTemplate.<E>wrapIfNecessary(e);
    } finally {
        close(retryPolicy, context, state, lastException == null || exhausted);
        doCloseInterceptors(retryCallback, context, lastException);
        RetrySynchronizationManager.clear();
    }
}
Also used : NoBackOffPolicy(org.springframework.retry.backoff.NoBackOffPolicy) BackOffPolicy(org.springframework.retry.backoff.BackOffPolicy) BackOffContext(org.springframework.retry.backoff.BackOffContext) RetryContext(org.springframework.retry.RetryContext) BackOffInterruptedException(org.springframework.retry.backoff.BackOffInterruptedException) SimpleRetryPolicy(org.springframework.retry.policy.SimpleRetryPolicy) RetryPolicy(org.springframework.retry.RetryPolicy) TerminatedRetryException(org.springframework.retry.TerminatedRetryException) BackOffInterruptedException(org.springframework.retry.backoff.BackOffInterruptedException) ExhaustedRetryException(org.springframework.retry.ExhaustedRetryException) RetryException(org.springframework.retry.RetryException) TerminatedRetryException(org.springframework.retry.TerminatedRetryException)

Aggregations

RetryContext (org.springframework.retry.RetryContext)2 TerminatedRetryException (org.springframework.retry.TerminatedRetryException)2 BackOffInterruptedException (org.springframework.retry.backoff.BackOffInterruptedException)2 Test (org.junit.Test)1 ExhaustedRetryException (org.springframework.retry.ExhaustedRetryException)1 RetryException (org.springframework.retry.RetryException)1 RetryPolicy (org.springframework.retry.RetryPolicy)1 BackOffContext (org.springframework.retry.backoff.BackOffContext)1 BackOffPolicy (org.springframework.retry.backoff.BackOffPolicy)1 NoBackOffPolicy (org.springframework.retry.backoff.NoBackOffPolicy)1 NeverRetryPolicy (org.springframework.retry.policy.NeverRetryPolicy)1 SimpleRetryPolicy (org.springframework.retry.policy.SimpleRetryPolicy)1