
io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties Maven / Gradle / Ivy
/*
* Copyright 2019 Dan Maas, Mahmoud Romeh
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.resilience4j.common.retry.configuration;
import io.github.resilience4j.common.CommonProperties;
import io.github.resilience4j.common.CompositeCustomizer;
import io.github.resilience4j.common.utils.ConfigUtils;
import io.github.resilience4j.core.ClassUtils;
import io.github.resilience4j.core.ConfigurationNotFoundException;
import io.github.resilience4j.core.IntervalFunction;
import io.github.resilience4j.core.StringUtils;
import io.github.resilience4j.core.lang.Nullable;
import io.github.resilience4j.retry.RetryConfig;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
/**
* Main spring properties for retry configuration
*/
public class RetryConfigurationProperties extends CommonProperties {
private final Map instances = new HashMap<>();
private Map configs = new HashMap<>();
/**
* @param backend backend name
* @return the retry configuration
*/
public RetryConfig createRetryConfig(String backend,
CompositeCustomizer compositeRetryCustomizer) {
return createRetryConfig(getBackendProperties(backend), compositeRetryCustomizer, backend);
}
/**
* @param backend retry backend name
* @return the configured spring backend properties
*/
@Nullable
public InstanceProperties getBackendProperties(String backend) {
return instances.get(backend);
}
/**
* @return the configured retry backend properties
*/
public Map getInstances() {
return instances;
}
/**
* For backwards compatibility when setting backends in configuration properties.
*/
public Map getBackends() {
return instances;
}
/**
* @return common configuration for retry backend
*/
public Map getConfigs() {
return configs;
}
/**
* @param instanceProperties the retry backend spring properties
* @return the retry configuration
*/
public RetryConfig createRetryConfig(InstanceProperties instanceProperties,
CompositeCustomizer compositeRetryCustomizer, String backend) {
if (instanceProperties != null && StringUtils
.isNotEmpty(instanceProperties.getBaseConfig())) {
InstanceProperties baseProperties = configs.get(instanceProperties.getBaseConfig());
if (baseProperties == null) {
throw new ConfigurationNotFoundException(instanceProperties.getBaseConfig());
}
return buildConfigFromBaseConfig(baseProperties, instanceProperties,
compositeRetryCustomizer, backend);
}
return buildRetryConfig(RetryConfig.custom(), instanceProperties, compositeRetryCustomizer,
backend);
}
private RetryConfig buildConfigFromBaseConfig(InstanceProperties baseProperties,
InstanceProperties instanceProperties,
CompositeCustomizer compositeRetryCustomizer,
String backend) {
RetryConfig baseConfig = buildRetryConfig(RetryConfig.custom(), baseProperties,
compositeRetryCustomizer, backend);
ConfigUtils.mergePropertiesIfAny(baseProperties, instanceProperties);
return buildRetryConfig(RetryConfig.from(baseConfig), instanceProperties,
compositeRetryCustomizer, backend);
}
/**
* @param properties the configured spring backend properties
* @return retry config builder instance
*/
@SuppressWarnings("unchecked")
private RetryConfig buildRetryConfig(RetryConfig.Builder builder,
InstanceProperties properties,
CompositeCustomizer compositeRetryCustomizer,
String backend) {
if (properties == null) {
return builder.build();
}
if (properties.enableExponentialBackoff != null && properties.enableExponentialBackoff
&& properties.enableRandomizedWait != null && properties.enableRandomizedWait) {
throw new IllegalStateException(
"you can not enable Exponential backoff policy and randomized delay at the same time , please enable only one of them");
}
configureRetryIntervalFunction(properties, builder);
if (properties.getMaxRetryAttempts() != null && properties.getMaxRetryAttempts() != 0) {
builder.maxAttempts(properties.getMaxRetryAttempts());
}
if (properties.getRetryExceptionPredicate() != null) {
if (properties.getRetryExceptionPredicate() != null) {
Predicate predicate = ClassUtils
.instantiatePredicateClass(properties.getRetryExceptionPredicate());
if (predicate != null) {
builder.retryOnException(predicate);
}
}
}
if (properties.getIgnoreExceptions() != null) {
builder.ignoreExceptions(properties.getIgnoreExceptions());
}
if (properties.getRetryExceptions() != null) {
builder.retryExceptions(properties.getRetryExceptions());
}
if (properties.getResultPredicate() != null) {
if (properties.getResultPredicate() != null) {
Predicate
© 2015 - 2025 Weber Informatics LLC | Privacy Policy