Retry policy added

This commit is contained in:
n.pospolita 2019-04-24 10:40:17 +03:00
parent d53dc1b88f
commit ebcb6d2714
2 changed files with 73 additions and 0 deletions

View File

@ -37,6 +37,11 @@
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

View File

@ -0,0 +1,68 @@
package com.rbkmoney.retry;
import lombok.Getter;
import org.springframework.classify.BinaryExceptionClassifier;
import org.springframework.retry.RetryContext;
import org.springframework.retry.RetryPolicy;
import org.springframework.retry.context.RetryContextSupport;
import org.springframework.util.ClassUtils;
import java.util.Map;
@Getter
public class InfiniteRetryPolicy implements RetryPolicy {
private final int maxAttempts;
private final BinaryExceptionClassifier retryableClassifier;
public InfiniteRetryPolicy(int maxAttempts, Map<Class<? extends Throwable>, Boolean> retryableExceptions) {
this(maxAttempts, retryableExceptions, false, false);
}
public InfiniteRetryPolicy(int maxAttempts, Map<Class<? extends Throwable>, Boolean> retryableExceptions,
boolean traverseCauses, boolean defaultValue) {
this.maxAttempts = maxAttempts;
this.retryableClassifier = new BinaryExceptionClassifier(retryableExceptions, defaultValue);
this.retryableClassifier.setTraverseCauses(traverseCauses);
}
public BinaryExceptionClassifier getRetryableClassifier() {
return retryableClassifier;
}
@Override
public boolean canRetry(RetryContext context) {
Throwable t = context.getLastThrowable();
return (t == null || retryForException(t)) && (maxAttempts < 0 || context.getRetryCount() < maxAttempts);
}
@Override
public RetryContext open(RetryContext parent) {
return new SimpleRetryContext(parent);
}
@Override
public void close(RetryContext context) {
}
@Override
public void registerThrowable(RetryContext context, Throwable throwable) {
((SimpleRetryContext) context).registerThrowable(throwable);
}
private static class SimpleRetryContext extends RetryContextSupport {
public SimpleRetryContext(RetryContext parent) {
super(parent);
}
}
private boolean retryForException(Throwable ex) {
return retryableClassifier.classify(ex);
}
@Override
public String toString() {
return ClassUtils.getShortName(getClass()) + "[maxAttempts=" + maxAttempts + "]";
}
}