Skip to content

Commit

Permalink
Issue resilience4j#1043: Allow the configuration of a IntervalBiFunct…
Browse files Browse the repository at this point in the history
…ion in Spring Boot2 (resilience4j#1235)
  • Loading branch information
cosminseceleanu committed Nov 6, 2020
1 parent 9aa18f8 commit bf636b9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ private ClassUtils() {
// utils
}

public static <T> IntervalBiFunction<T> instantiateIntervalBiFunctionClass(
Class<? extends IntervalBiFunction<T>> clazz) {
try {
Constructor<? extends IntervalBiFunction<T>> c = clazz.getConstructor();
if (c != null) {
return c.newInstance();
} else {
throw new InstantiationException(INSTANTIATION_ERROR_PREFIX + clazz.getName());
}
} catch (Exception e) {
throw new InstantiationException(INSTANTIATION_ERROR_PREFIX + clazz.getName(), e);
}
}

public static <T> Predicate<T> instantiatePredicateClass(Class<? extends Predicate<T>> clazz) {
try {
Constructor<? extends Predicate<T>> c = clazz.getConstructor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.github.resilience4j.core.ClassUtils;
import io.github.resilience4j.core.ConfigurationNotFoundException;
import io.github.resilience4j.core.IntervalFunction;
import io.github.resilience4j.core.IntervalBiFunction;
import io.github.resilience4j.core.StringUtils;
import io.github.resilience4j.core.lang.Nullable;
import io.github.resilience4j.retry.RetryConfig;
Expand Down Expand Up @@ -150,6 +151,11 @@ private RetryConfig buildRetryConfig(RetryConfig.Builder builder,
.instantiatePredicateClass(properties.getResultPredicate());
builder.retryOnResult(predicate);
}
if (properties.getIntervalBiFunction() != null) {
IntervalBiFunction<Object> intervalBiFunction = ClassUtils
.instantiateIntervalBiFunctionClass(properties.getIntervalBiFunction());
builder.intervalBiFunction(intervalBiFunction);
}

compositeRetryCustomizer.getCustomizer(backend)
.ifPresent(customizer -> customizer.customize(builder));
Expand Down Expand Up @@ -243,6 +249,12 @@ public static class InstanceProperties {
@Nullable
private Duration waitDuration;

/*
* retry intervalBiFunction class to be used to calculate wait based on exception or result
*/
@Nullable
private Class<? extends IntervalBiFunction<Object>> intervalBiFunction;

/*
* max retry attempts value
*/
Expand Down Expand Up @@ -331,6 +343,15 @@ public InstanceProperties setWaitDuration(Duration waitDuration) {
return this;
}

@Nullable
public Class<? extends IntervalBiFunction<Object>> getIntervalBiFunction() {
return intervalBiFunction;
}

public void setIntervalBiFunction(Class<? extends IntervalBiFunction<Object>> intervalBiFunction) {
this.intervalBiFunction = intervalBiFunction;
}

@Nullable
@Deprecated
public Integer getMaxRetryAttempts() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.resilience4j.common;

import io.github.resilience4j.core.IntervalBiFunction;
import io.vavr.control.Either;

public class TestIntervalBiFunction implements IntervalBiFunction<Object> {
@Override
public Long apply(Integer integer, Either<Throwable, Object> objects) {
return 200L;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.github.resilience4j.common.CompositeCustomizer;
import io.github.resilience4j.common.RecordFailurePredicate;
import io.github.resilience4j.common.TestIntervalBiFunction;
import io.github.resilience4j.core.ConfigurationNotFoundException;
import io.github.resilience4j.retry.RetryConfig;
import org.junit.Test;
Expand Down Expand Up @@ -190,4 +191,19 @@ public void testIllegalArgumentOnMaxAttempts() {
private CompositeCustomizer<RetryConfigCustomizer> compositeRetryCustomizer() {
return new CompositeCustomizer<>(Collections.emptyList());
}

@Test
public void testIntervalBiFunctionConfig() {
RetryConfigurationProperties.InstanceProperties instanceProperties = new RetryConfigurationProperties.InstanceProperties();
instanceProperties.setIntervalBiFunction(TestIntervalBiFunction.class);

RetryConfigurationProperties retryConfigurationProperties = new RetryConfigurationProperties();
retryConfigurationProperties.getInstances().put("backend", instanceProperties);

RetryConfig retryConfig = retryConfigurationProperties
.createRetryConfig("backend", compositeRetryCustomizer());

assertThat(retryConfig.getIntervalBiFunction()).isNotNull();
assertThat(retryConfig.getIntervalBiFunction()).isExactlyInstanceOf(TestIntervalBiFunction.class);
}
}

0 comments on commit bf636b9

Please sign in to comment.