Search in sources :

Example 1 with ExpressionRetryPolicy

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

the class AnnotationAwareRetryOperationsInterceptor method getRetryPolicy.

private RetryPolicy getRetryPolicy(Annotation retryable) {
    Map<String, Object> attrs = AnnotationUtils.getAnnotationAttributes(retryable);
    @SuppressWarnings("unchecked") Class<? extends Throwable>[] includes = (Class<? extends Throwable>[]) attrs.get("value");
    String exceptionExpression = (String) attrs.get("exceptionExpression");
    boolean hasExpression = StringUtils.hasText(exceptionExpression);
    if (includes.length == 0) {
        @SuppressWarnings("unchecked") Class<? extends Throwable>[] value = (Class<? extends Throwable>[]) attrs.get("include");
        includes = value;
    }
    @SuppressWarnings("unchecked") Class<? extends Throwable>[] excludes = (Class<? extends Throwable>[]) attrs.get("exclude");
    Integer maxAttempts = (Integer) attrs.get("maxAttempts");
    String maxAttemptsExpression = (String) attrs.get("maxAttemptsExpression");
    if (StringUtils.hasText(maxAttemptsExpression)) {
        if (ExpressionRetryPolicy.isTemplate(maxAttemptsExpression)) {
            maxAttempts = PARSER.parseExpression(resolve(maxAttemptsExpression), PARSER_CONTEXT).getValue(this.evaluationContext, Integer.class);
        } else {
            maxAttempts = PARSER.parseExpression(resolve(maxAttemptsExpression)).getValue(this.evaluationContext, Integer.class);
        }
    }
    if (includes.length == 0 && excludes.length == 0) {
        SimpleRetryPolicy simple = hasExpression ? new ExpressionRetryPolicy(resolve(exceptionExpression)).withBeanFactory(this.beanFactory) : new SimpleRetryPolicy();
        simple.setMaxAttempts(maxAttempts);
        return simple;
    }
    Map<Class<? extends Throwable>, Boolean> policyMap = new HashMap<Class<? extends Throwable>, Boolean>();
    for (Class<? extends Throwable> type : includes) {
        policyMap.put(type, true);
    }
    for (Class<? extends Throwable> type : excludes) {
        policyMap.put(type, false);
    }
    boolean retryNotExcluded = includes.length == 0;
    if (hasExpression) {
        return new ExpressionRetryPolicy(maxAttempts, policyMap, true, exceptionExpression, retryNotExcluded).withBeanFactory(this.beanFactory);
    } else {
        return new SimpleRetryPolicy(maxAttempts, policyMap, true, retryNotExcluded);
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentReferenceHashMap(cn.taketoday.util.ConcurrentReferenceHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) ExpressionRetryPolicy(cn.taketoday.retry.policy.ExpressionRetryPolicy) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 2 with ExpressionRetryPolicy

use of cn.taketoday.retry.policy.ExpressionRetryPolicy in project today-infrastructure by TAKETODAY.

the class AnnotationAwareRetryOperationsInterceptor method getRetryPolicy.

private RetryPolicy getRetryPolicy(Annotation retryable) {
    Map<String, Object> attrs = AnnotationUtils.getAnnotationAttributes(retryable);
    @SuppressWarnings("unchecked") Class<? extends Throwable>[] includes = (Class<? extends Throwable>[]) attrs.get("value");
    String exceptionExpression = (String) attrs.get("exceptionExpression");
    boolean hasExpression = StringUtils.hasText(exceptionExpression);
    if (includes.length == 0) {
        @SuppressWarnings("unchecked") Class<? extends Throwable>[] value = (Class<? extends Throwable>[]) attrs.get("include");
        includes = value;
    }
    @SuppressWarnings("unchecked") Class<? extends Throwable>[] excludes = (Class<? extends Throwable>[]) attrs.get("exclude");
    Integer maxAttempts = (Integer) attrs.get("maxAttempts");
    String maxAttemptsExpression = (String) attrs.get("maxAttemptsExpression");
    if (StringUtils.hasText(maxAttemptsExpression)) {
        if (ExpressionRetryPolicy.isTemplate(maxAttemptsExpression)) {
            maxAttempts = PARSER.parseExpression(resolve(maxAttemptsExpression), PARSER_CONTEXT).getValue(this.evaluationContext, Integer.class);
        } else {
            maxAttempts = PARSER.parseExpression(resolve(maxAttemptsExpression)).getValue(this.evaluationContext, Integer.class);
        }
    }
    if (includes.length == 0 && excludes.length == 0) {
        SimpleRetryPolicy simple = hasExpression ? new ExpressionRetryPolicy(resolve(exceptionExpression)).withBeanFactory(this.beanFactory) : new SimpleRetryPolicy();
        simple.setMaxAttempts(maxAttempts);
        return simple;
    }
    Map<Class<? extends Throwable>, Boolean> policyMap = new HashMap<Class<? extends Throwable>, Boolean>();
    for (Class<? extends Throwable> type : includes) {
        policyMap.put(type, true);
    }
    for (Class<? extends Throwable> type : excludes) {
        policyMap.put(type, false);
    }
    boolean retryNotExcluded = includes.length == 0;
    if (hasExpression) {
        return new ExpressionRetryPolicy(maxAttempts, policyMap, true, exceptionExpression, retryNotExcluded).withBeanFactory(this.beanFactory);
    } else {
        return new SimpleRetryPolicy(maxAttempts, policyMap, true, retryNotExcluded);
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentReferenceHashMap(cn.taketoday.util.ConcurrentReferenceHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) ExpressionRetryPolicy(cn.taketoday.retry.policy.ExpressionRetryPolicy) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Aggregations

ExpressionRetryPolicy (cn.taketoday.retry.policy.ExpressionRetryPolicy)2 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)2 ConcurrentReferenceHashMap (cn.taketoday.util.ConcurrentReferenceHashMap)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2