From 8b35d06cf6112bc51135c0d3496e7f712de021f6 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 20 Jun 2018 09:28:00 +0200 Subject: [PATCH] Share RetryTemplate infrastructure for Rabbit listener and template Closes gh-13529 --- ...bitListenerContainerFactoryConfigurer.java | 5 +- .../amqp/RabbitAutoConfiguration.java | 22 +------- .../amqp/RetryTemplateFactory.java | 50 +++++++++++++++++++ 3 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RetryTemplateFactory.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/AbstractRabbitListenerContainerFactoryConfigurer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/AbstractRabbitListenerContainerFactoryConfigurer.java index 8475293207..989a43bbe5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/AbstractRabbitListenerContainerFactoryConfigurer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/AbstractRabbitListenerContainerFactoryConfigurer.java @@ -107,9 +107,8 @@ public abstract class AbstractRabbitListenerContainerFactoryConfigurer 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); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java index d08d1b2c76..2895a5f8a4 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java @@ -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) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RetryTemplateFactory.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RetryTemplateFactory.java new file mode 100644 index 0000000000..a98adca394 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RetryTemplateFactory.java @@ -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; + } + +}