/** * Decide whether to proceed with the ongoing retry attempt. This method is called * before the {@link RetryCallback} is executed, but after the backoff and open * interceptors. * * @param retryPolicy the policy to apply * @param context the current retry context * @return true if we can continue with the attempt */ protected boolean canRetry(RetryPolicy retryPolicy, RetryContext context) { return retryPolicy.canRetry(context); }
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; }
/** * Delegate to the policy currently activated in the context. * * @see org.springframework.retry.RetryPolicy#registerThrowable(org.springframework.retry.RetryContext, * Throwable) */ public void registerThrowable(RetryContext context, Throwable throwable) { RetryPolicy policy = (RetryPolicy) context; policy.registerThrowable(context, throwable); ((RetryContextSupport) context).registerThrowable(throwable); }
/** * @param retryPolicy * @param ex * @return */ private SimpleRetryExceptionHandler getHandlerAfterRetry(RetryPolicy retryPolicy, RuntimeException ex, Collection<Class<? extends Throwable>> fatalExceptions) { // Always rethrow if the retry is exhausted SimpleRetryExceptionHandler handler = new SimpleRetryExceptionHandler(retryPolicy, new SimpleLimitExceptionHandler(0), fatalExceptions); // Simulate a failed retry... RetryContext retryContext = retryPolicy.open(null); retryPolicy.registerThrowable(retryContext, ex); handler.close(retryContext, null, ex); return handler; }
@Override public void close(RetryContext context) { CircuitBreakerRetryContext circuit = (CircuitBreakerRetryContext) context; this.delegate.close(circuit.context); }
/** * Delegate to the policy currently activated in the context. * * @see org.springframework.retry.RetryPolicy#close(org.springframework.retry.RetryContext) */ public void close(RetryContext context) { RetryPolicy policy = (RetryPolicy) context; policy.close(context); }
/** * Delegate to the policy currently activated in the context. * * @see org.springframework.retry.RetryPolicy#canRetry(org.springframework.retry.RetryContext) */ public boolean canRetry(RetryContext context) { RetryPolicy policy = (RetryPolicy) context; return policy.canRetry(context); }
@Override public void registerThrowable(RetryContext context, Throwable throwable) { CircuitBreakerRetryContext circuit = (CircuitBreakerRetryContext) context; circuit.registerThrowable(throwable); this.delegate.registerThrowable(circuit.context, throwable); }
private RetryContext createDelegateContext(RetryPolicy policy, RetryContext parent) { RetryContext context = policy.open(parent); reset(); return context; }
/** * Delegate to the policies that were in operation when the context was * created. If any of them fails to close the exception is propagated (and * those later in the chain are closed before re-throwing). * * @see org.springframework.retry.RetryPolicy#close(org.springframework.retry.RetryContext) * @param context the {@link RetryContext} */ @Override public void close(RetryContext context) { RetryContext[] contexts = ((CompositeRetryContext) context).contexts; RetryPolicy[] policies = ((CompositeRetryContext) context).policies; RuntimeException exception = null; for (int i = 0; i < contexts.length; i++) { try { policies[i].close(contexts[i]); } catch (RuntimeException e) { if (exception == null) { exception = e; } } } if (exception != null) { throw exception; } }
public boolean canRetry(RetryContext context) { return this.context == null || policy.canRetry(this.context); }
protected void registerThrowable(RetryPolicy retryPolicy, RetryState state, RetryContext context, Throwable e) { retryPolicy.registerThrowable(context, e); registerContext(context, state); }
/** * Creates a new context that copies the existing policies and keeps a list * of the contexts from each one. * * @see org.springframework.retry.RetryPolicy#open(RetryContext) */ @Override public RetryContext open(RetryContext parent) { List<RetryContext> list = new ArrayList<RetryContext>(); for (RetryPolicy policy : this.policies) { list.add(policy.open(parent)); } return new CompositeRetryContext(parent, list, this.policies); }
public void close(RetryContext context) { // Only close those policies that have been used (opened): for (RetryPolicy policy : contexts.keySet()) { policy.close(getContext(policy, context.getParent())); } }
public boolean canRetry(RetryContext context) { return context==null ? true : retryPolicy.canRetry(context); }
/** * Delegate to the policies that were in operation when the context was * created. * * @see org.springframework.retry.RetryPolicy#close(org.springframework.retry.RetryContext) */ @Override public void registerThrowable(RetryContext context, Throwable throwable) { RetryContext[] contexts = ((CompositeRetryContext) context).contexts; RetryPolicy[] policies = ((CompositeRetryContext) context).policies; for (int i = 0; i < contexts.length; i++) { policies[i].registerThrowable(contexts[i], throwable); } ((RetryContextSupport) context).registerThrowable(throwable); }
private RetryContext doOpenInternal(RetryPolicy retryPolicy, RetryState state) { RetryContext context = retryPolicy.open(RetrySynchronizationManager.getContext()); if (state != null) { context.setAttribute(RetryContext.STATE_KEY, state.getKey()); } if (context.hasAttribute(GLOBAL_STATE)) { registerContext(context, state); } return context; }
/** * Clean up the cache if necessary and close the context provided (if the flag * indicates that processing was successful). * * @param retryPolicy the {@link RetryPolicy} * @param context the {@link RetryContext} * @param state the {@link RetryState} * @param succeeded whether the close succeeded */ protected void close(RetryPolicy retryPolicy, RetryContext context, RetryState state, boolean succeeded) { if (state != null) { if (succeeded) { if (!context.hasAttribute(GLOBAL_STATE)) { this.retryContextCache.remove(state.getKey()); } retryPolicy.close(context); context.setAttribute(RetryContext.CLOSED, true); } } else { retryPolicy.close(context); context.setAttribute(RetryContext.CLOSED, true); } }
/** * Delegate to the policies that were in operation when the context was * created. If any of them cannot retry then return false, otherwise return * true. * * @param context the {@link RetryContext} * @see org.springframework.retry.RetryPolicy#canRetry(org.springframework.retry.RetryContext) */ @Override public boolean canRetry(RetryContext context) { RetryContext[] contexts = ((CompositeRetryContext) context).contexts; RetryPolicy[] policies = ((CompositeRetryContext) context).policies; boolean retryable = true; if(this.optimistic) { retryable = false; for (int i = 0; i < contexts.length; i++) { if (policies[i].canRetry(contexts[i])) { retryable = true; } } } else { for (int i = 0; i < contexts.length; i++) { if (!policies[i].canRetry(contexts[i])) { retryable = false; } } } return retryable; }
public void registerThrowable(RetryContext context, Throwable throwable) { policy = exceptionClassifier.classify(throwable); Assert.notNull(policy, "Could not locate policy for exception=[" + throwable + "]."); this.context = getContext(policy, context.getParent()); policy.registerThrowable(this.context, throwable); }