Share RetryTemplate infrastructure for Rabbit listener and template
Closes gh-13529
This commit is contained in:
parent
65cc7c72f4
commit
8b35d06cf6
|
@ -107,9 +107,8 @@ public abstract class AbstractRabbitListenerContainerFactoryConfigurer<T extends
|
|||
RetryInterceptorBuilder<?> builder = (retryConfig.isStateless()
|
||||
? RetryInterceptorBuilder.stateless()
|
||||
: RetryInterceptorBuilder.stateful());
|
||||
builder.maxAttempts(retryConfig.getMaxAttempts());
|
||||
builder.backOffOptions(retryConfig.getInitialInterval().toMillis(),
|
||||
retryConfig.getMultiplier(), retryConfig.getMaxInterval().toMillis());
|
||||
builder.retryOperations(
|
||||
new RetryTemplateFactory().createRetryTemplate(retryConfig));
|
||||
MessageRecoverer recoverer = (this.messageRecoverer != null
|
||||
? this.messageRecoverer : new RejectAndDontRequeueRecoverer());
|
||||
builder.recoverer(recoverer);
|
||||
|
|
|
@ -40,9 +40,6 @@ import org.springframework.boot.context.properties.PropertyMapper;
|
|||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
|
||||
import org.springframework.retry.policy.SimpleRetryPolicy;
|
||||
import org.springframework.retry.support.RetryTemplate;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for {@link RabbitTemplate}.
|
||||
|
@ -178,7 +175,8 @@ public class RabbitAutoConfiguration {
|
|||
template.setMandatory(determineMandatoryFlag());
|
||||
RabbitProperties.Template properties = this.properties.getTemplate();
|
||||
if (properties.getRetry().isEnabled()) {
|
||||
template.setRetryTemplate(createRetryTemplate(properties.getRetry()));
|
||||
template.setRetryTemplate(new RetryTemplateFactory()
|
||||
.createRetryTemplate(properties.getRetry()));
|
||||
}
|
||||
map.from(properties::getReceiveTimeout).whenNonNull().as(Duration::toMillis)
|
||||
.to(template::setReceiveTimeout);
|
||||
|
@ -194,22 +192,6 @@ public class RabbitAutoConfiguration {
|
|||
return (mandatory != null ? mandatory : this.properties.isPublisherReturns());
|
||||
}
|
||||
|
||||
private RetryTemplate createRetryTemplate(RabbitProperties.Retry properties) {
|
||||
PropertyMapper map = PropertyMapper.get();
|
||||
RetryTemplate template = new RetryTemplate();
|
||||
SimpleRetryPolicy policy = new SimpleRetryPolicy();
|
||||
map.from(properties::getMaxAttempts).to(policy::setMaxAttempts);
|
||||
template.setRetryPolicy(policy);
|
||||
ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
|
||||
map.from(properties::getInitialInterval).whenNonNull().as(Duration::toMillis)
|
||||
.to(backOffPolicy::setInitialInterval);
|
||||
map.from(properties::getMultiplier).to(backOffPolicy::setMultiplier);
|
||||
map.from(properties::getMaxInterval).whenNonNull().as(Duration::toMillis)
|
||||
.to(backOffPolicy::setMaxInterval);
|
||||
template.setBackOffPolicy(backOffPolicy);
|
||||
return template;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnSingleCandidate(ConnectionFactory.class)
|
||||
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "dynamic", matchIfMissing = true)
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.boot.autoconfigure.amqp;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.springframework.boot.context.properties.PropertyMapper;
|
||||
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
|
||||
import org.springframework.retry.policy.SimpleRetryPolicy;
|
||||
import org.springframework.retry.support.RetryTemplate;
|
||||
|
||||
/**
|
||||
* Factory to create {@link RetryTemplate} instance from properties defined in
|
||||
* {@link RabbitProperties}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class RetryTemplateFactory {
|
||||
|
||||
public RetryTemplate createRetryTemplate(RabbitProperties.Retry properties) {
|
||||
PropertyMapper map = PropertyMapper.get();
|
||||
RetryTemplate template = new RetryTemplate();
|
||||
SimpleRetryPolicy policy = new SimpleRetryPolicy();
|
||||
map.from(properties::getMaxAttempts).to(policy::setMaxAttempts);
|
||||
template.setRetryPolicy(policy);
|
||||
ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
|
||||
map.from(properties::getInitialInterval).whenNonNull().as(Duration::toMillis)
|
||||
.to(backOffPolicy::setInitialInterval);
|
||||
map.from(properties::getMultiplier).to(backOffPolicy::setMultiplier);
|
||||
map.from(properties::getMaxInterval).whenNonNull().as(Duration::toMillis)
|
||||
.to(backOffPolicy::setMaxInterval);
|
||||
template.setBackOffPolicy(backOffPolicy);
|
||||
return template;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue