Package org.springframework.retry

Examples of org.springframework.retry.RetryContext


      this.context = getContext(policy, context.getParent());
      policy.registerThrowable(this.context, throwable);
    }

    private RetryContext getContext(RetryPolicy policy, RetryContext parent) {
      RetryContext context = contexts.get(policy);
      if (context == null) {
        context = policy.open(parent);
        contexts.put(policy, context);
      }
      return context;
View Full Code Here


   * Public accessor for the locally enclosing {@link RetryContext}.
   *
   * @return the current retry context, or null if there isn't one
   */
  public static RetryContext getContext() {
    RetryContext result = (RetryContext) context.get();
    return result;
  }
View Full Code Here

   *
   * @param context the new context to register
   * @return the old context if there was one
   */
  public static RetryContext register(RetryContext context) {
    RetryContext oldContext = getContext();
    RetrySynchronizationManager.context.set(context);
    return oldContext;
  }
View Full Code Here

   * {@link RetryOperations} implementations.
   *
   * @return the old value if there was one.
   */
  public static RetryContext clear() {
    RetryContext value = getContext();
    RetryContext parent = value == null ? null : value.getParent();
    RetrySynchronizationManager.context.set(parent);
    return value;
  }
View Full Code Here

    RetryPolicy retryPolicy = this.retryPolicy;
    BackOffPolicy backOffPolicy = this.backOffPolicy;

    // Allow the retry policy to initialise itself...
    RetryContext context = open(retryPolicy, state);
    if (logger.isTraceEnabled()) {
      logger.trace("RetryContext retrieved: " + context);
    }

    // Make sure the context is available globally for clients who need
    // it...
    RetrySynchronizationManager.register(context);

    Throwable lastException = null;

    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 (logger.isDebugEnabled()) {
            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;

          doOnErrorInterceptors(retryCallback, context, e);

          try {
            registerThrowable(retryPolicy, state, context, e);
          }
          catch (Exception ex) {
            throw new TerminatedRetryException("Could not register throwable", ex);
          }

          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 (logger.isDebugEnabled()) {
                logger.debug("Abort retry because interrupted: count=" + context.getRetryCount());
              }
              throw ex;
            }
          }

          if (logger.isDebugEnabled()) {
            logger.debug("Checking for rethrow: count=" + context.getRetryCount());
          }

          if (shouldRethrow(retryPolicy, context, state)) {
            if (logger.isDebugEnabled()) {
              logger.debug("Rethrow in retry for policy: count=" + context.getRetryCount());
            }
            throw RetryTemplate.<E>wrapIfNecessary(e);
          }

        }

        /*
         * A stateful attempt that can retry should have rethrown the exception by
         * now - i.e. we shouldn't get this far for a stateful attempt if it can
         * retry.
         */
      }

      if (logger.isDebugEnabled()) {
        logger.debug("Retry failed last attempt: count=" + context.getRetryCount());
      }

      if (context.isExhaustedOnly()) {
        rethrow(context, "Retry exhausted after last attempt with no recovery path.");
      }

      return handleRetryExhausted(recoveryCallback, context, state);

View Full Code Here

    if (!retryContextCache.containsKey(key)) {
      // The cache is only used if there is a failure.
      return doOpenInternal(retryPolicy);
    }

    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, "
View Full Code Here

public class AlwaysRetryPolicyTests {

  @Test
  public void testSimpleOperations() throws Exception {
    AlwaysRetryPolicy policy = new AlwaysRetryPolicy();
    RetryContext context = policy.open(null);
    assertNotNull(context);
    assertTrue(policy.canRetry(context));
    policy.registerThrowable(context, null);
    assertTrue(policy.canRetry(context));
    policy.close(context);
View Full Code Here

  }

  @Test
  public void testRetryCount() throws Exception {
    AlwaysRetryPolicy policy = new AlwaysRetryPolicy();
    RetryContext context = policy.open(null);
    assertNotNull(context);
    policy.registerThrowable(context, null);
    assertEquals(0, context.getRetryCount());
    policy.registerThrowable(context, new RuntimeException("foo"));
    assertEquals(1, context.getRetryCount());
    assertEquals("foo", context.getLastThrowable().getMessage());
  }
View Full Code Here

  }

  @Test
  public void testParent() throws Exception {
    AlwaysRetryPolicy policy = new AlwaysRetryPolicy();
    RetryContext context = policy.open(null);
    RetryContext child = policy.open(context);
    assertNotSame(child, context);
    assertSame(context, child.getParent());
  }
View Full Code Here

  private ExceptionClassifierRetryPolicy policy = new ExceptionClassifierRetryPolicy();

  @Test
  public void testDefaultPolicies() throws Exception {
    RetryContext context = policy.open(null);
    assertNotNull(context);
  }
View Full Code Here

TOP

Related Classes of org.springframework.retry.RetryContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.