Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* 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.
Expand All @@ -24,6 +24,7 @@
import org.springframework.amqp.rabbit.retry.RejectAndDontRequeueRecoverer;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties.ListenerRetry;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.util.Assert;

/**
Expand All @@ -42,6 +43,8 @@ public abstract class AbstractRabbitListenerContainerFactoryConfigurer<T extends

private RabbitProperties rabbitProperties;

private RabbitListenerRetryTemplateCustomizer retryTemplateCustomizer;

/**
* Set the {@link MessageConverter} to use or {@code null} if the out-of-the-box
* converter should be used.
Expand All @@ -59,6 +62,14 @@ protected void setMessageRecoverer(MessageRecoverer messageRecoverer) {
this.messageRecoverer = messageRecoverer;
}

/**
* Set the {@link RabbitListenerRetryTemplateCustomizer} to use.
* @param retryTemplateCustomizer the customizer
*/
public void setRetryTemplateCustomizer(RabbitListenerRetryTemplateCustomizer retryTemplateCustomizer) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've started a separate discussion (#13530) but I wonder if a Consumer<RetryTemplate> wouldn't be enough here.

this.retryTemplateCustomizer = retryTemplateCustomizer;
}

/**
* Set the {@link RabbitProperties} to use.
* @param rabbitProperties the {@link RabbitProperties}
Expand Down Expand Up @@ -107,9 +118,9 @@ protected void configure(T factory, ConnectionFactory connectionFactory,
RetryInterceptorBuilder<?> builder = (retryConfig.isStateless()
? RetryInterceptorBuilder.stateless()
: RetryInterceptorBuilder.stateful());
builder.maxAttempts(retryConfig.getMaxAttempts());
builder.backOffOptions(retryConfig.getInitialInterval(),
retryConfig.getMultiplier(), retryConfig.getMaxInterval());
RetryTemplate template = RabbitAutoConfiguration.createRetryTemplate(retryConfig,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this PR offers a way to customize the retry template and to set it at all. To me, these are two separate features that deserve a separate commit. I've just done that in #13529 (calling the auto-config that way isn't something we do, see the commit for more details).

this.retryTemplateCustomizer);
builder.retryOperations(template);
MessageRecoverer recoverer = (this.messageRecoverer != null
? this.messageRecoverer : new RejectAndDontRequeueRecoverer());
builder.recoverer(recoverer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* 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.
Expand Down Expand Up @@ -45,13 +45,17 @@ class RabbitAnnotationDrivenConfiguration {

private final ObjectProvider<MessageRecoverer> messageRecoverer;

private final ObjectProvider<RabbitListenerRetryTemplateCustomizer> retryTemplateCustomizer;

private final RabbitProperties properties;

RabbitAnnotationDrivenConfiguration(ObjectProvider<MessageConverter> messageConverter,
ObjectProvider<MessageRecoverer> messageRecoverer,
ObjectProvider<RabbitListenerRetryTemplateCustomizer> retryCustomizer,
RabbitProperties properties) {
this.messageConverter = messageConverter;
this.messageRecoverer = messageRecoverer;
this.retryTemplateCustomizer = retryCustomizer;
this.properties = properties;
}

Expand All @@ -61,6 +65,7 @@ public SimpleRabbitListenerContainerFactoryConfigurer simpleRabbitListenerContai
SimpleRabbitListenerContainerFactoryConfigurer configurer = new SimpleRabbitListenerContainerFactoryConfigurer();
configurer.setMessageConverter(this.messageConverter.getIfUnique());
configurer.setMessageRecoverer(this.messageRecoverer.getIfUnique());
configurer.setRetryTemplateCustomizer(this.retryTemplateCustomizer.getIfUnique());
configurer.setRabbitProperties(this.properties);
return configurer;
}
Expand All @@ -82,6 +87,7 @@ public DirectRabbitListenerContainerFactoryConfigurer directRabbitListenerContai
DirectRabbitListenerContainerFactoryConfigurer configurer = new DirectRabbitListenerContainerFactoryConfigurer();
configurer.setMessageConverter(this.messageConverter.getIfUnique());
configurer.setMessageRecoverer(this.messageRecoverer.getIfUnique());
configurer.setRetryTemplateCustomizer(this.retryTemplateCustomizer.getIfUnique());
configurer.setRabbitProperties(this.properties);
return configurer;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* 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.
Expand Down Expand Up @@ -34,6 +34,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.retry.RetryTemplateCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -151,12 +152,16 @@ protected static class RabbitTemplateConfiguration {

private final ObjectProvider<MessageConverter> messageConverter;

private final ObjectProvider<RabbitTemplateRetryTemplateCustomizer> retryTemplateCustomizer;

private final RabbitProperties properties;

public RabbitTemplateConfiguration(
ObjectProvider<MessageConverter> messageConverter,
ObjectProvider<RabbitTemplateRetryTemplateCustomizer> retryTemplateCustomizer,
RabbitProperties properties) {
this.messageConverter = messageConverter;
this.retryTemplateCustomizer = retryTemplateCustomizer;
this.properties = properties;
}

Expand All @@ -173,7 +178,8 @@ public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
template.setMandatory(determineMandatoryFlag());
RabbitProperties.Template properties = this.properties.getTemplate();
if (properties.getRetry().isEnabled()) {
template.setRetryTemplate(createRetryTemplate(properties.getRetry()));
template.setRetryTemplate(createRetryTemplate(properties.getRetry(),
this.retryTemplateCustomizer.getIfUnique()));
}
map.from(properties::getReceiveTimeout).whenNonNull()
.to(template::setReceiveTimeout);
Expand All @@ -189,21 +195,6 @@ private boolean determineMandatoryFlag() {
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)
.to(backOffPolicy::setInitialInterval);
map.from(properties::getMultiplier).to(backOffPolicy::setMultiplier);
map.from(properties::getMaxInterval).to(backOffPolicy::setMaxInterval);
template.setBackOffPolicy(backOffPolicy);
return template;
}

@Bean
@ConditionalOnSingleCandidate(ConnectionFactory.class)
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "dynamic", matchIfMissing = true)
Expand All @@ -229,4 +220,23 @@ public RabbitMessagingTemplate rabbitMessagingTemplate(

}

static RetryTemplate createRetryTemplate(RabbitProperties.Retry properties,
RetryTemplateCustomizer customizer) {
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)
.to(backOffPolicy::setInitialInterval);
map.from(properties::getMultiplier).to(backOffPolicy::setMultiplier);
map.from(properties::getMaxInterval).to(backOffPolicy::setMaxInterval);
template.setBackOffPolicy(backOffPolicy);
if (customizer != null) {
customizer.customize(template);
}
return template;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 org.springframework.boot.autoconfigure.retry.RetryTemplateCustomizer;

/**
* A marker interface for a {@link RetryTemplateCustomizer} for RabbitMQ listeners.
*
* @author Gary Russell
* @since 2.0
*
*/
public interface RabbitListenerRetryTemplateCustomizer extends RetryTemplateCustomizer {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 org.springframework.boot.autoconfigure.retry.RetryTemplateCustomizer;

/**
* A marker interface for a {@link RetryTemplateCustomizer} for RabbitMQ templates.
*
* @author Gary Russell
* @since 2.0
*
*/
public interface RabbitTemplateRetryTemplateCustomizer extends RetryTemplateCustomizer {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.retry;

import org.springframework.retry.support.RetryTemplate;

/**
* A customizer for a {@link RetryTemplate}, for example, to add a RetryListener.
*
* @author Gary Russell
* @since 2.0
*
*/
public interface RetryTemplateCustomizer {

/**
* Customize the template.
* @param template the template.
* @param properties the properties.
*/
void customize(RetryTemplate template);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.
*/

/**
* Auto-configuration for Spring Retry.
*/
package org.springframework.boot.autoconfigure.retry;
Loading