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
Expand Up @@ -154,9 +154,11 @@ public RabbitTemplateConfigurer rabbitTemplateConfigurer(RabbitProperties proper
@Bean
@ConditionalOnSingleCandidate(ConnectionFactory.class)
@ConditionalOnMissingBean(RabbitOperations.class)
public RabbitTemplate rabbitTemplate(RabbitTemplateConfigurer configurer, ConnectionFactory connectionFactory) {
public RabbitTemplate rabbitTemplate(RabbitTemplateConfigurer configurer, ConnectionFactory connectionFactory,
ObjectProvider<RabbitTemplateCustomizer> rabbitTemplateCustomizers) {
RabbitTemplate template = new RabbitTemplate();
configurer.configure(template, connectionFactory);
rabbitTemplateCustomizers.orderedStream().forEach((customizer) -> customizer.customize(template));
return template;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2012-2022 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
*
* https://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.amqp.rabbit.core.RabbitTemplate;

/**
* Callback interface that can be used to customize a {@link RabbitTemplate}.
*
* @author dang zhicairang
* @since 2.3.0
*/
@FunctionalInterface
public interface RabbitTemplateCustomizer {

/**
* Customize the {@link RabbitTemplate}, such as used in
* {@link RabbitAutoConfiguration}.
* @param rabbitTemplate the rabbitTemplate to customize
*/
void customize(RabbitTemplate rabbitTemplate);

}
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,22 @@ void testRabbitTemplateConfigurerUsesConfig() {
});
}

@Test
void whenMultipleRabbitTemplateCustomizersAreDefinedThenTheyAreCalledInOrder() {
this.contextRunner.withUserConfiguration(MultipleRabbitTemplateCustomizersConfiguration.class)
.run((context) -> {
RabbitTemplateCustomizer firstCustomizer = context.getBean("firstCustomizer",
RabbitTemplateCustomizer.class);
RabbitTemplateCustomizer secondCustomizer = context.getBean("secondCustomizer",
RabbitTemplateCustomizer.class);
InOrder inOrder = inOrder(firstCustomizer, secondCustomizer);
RabbitTemplate template = context.getBean(RabbitTemplate.class);
then(firstCustomizer).should(inOrder).customize(template);
then(secondCustomizer).should(inOrder).customize(template);
inOrder.verifyNoMoreInteractions();
});
}

@Test
void testConnectionFactoryBackOff() {
this.contextRunner.withUserConfiguration(TestConfiguration2.class).run((context) -> {
Expand Down Expand Up @@ -972,6 +988,23 @@ MessageRecoverer anotherMessageRecoverer() {

}

@Configuration(proxyBeanMethods = false)
static class MultipleRabbitTemplateCustomizersConfiguration {

@Bean
@Order(Ordered.LOWEST_PRECEDENCE)
RabbitTemplateCustomizer secondCustomizer() {
return mock(RabbitTemplateCustomizer.class);
}

@Bean
@Order(0)
RabbitTemplateCustomizer firstCustomizer() {
return mock(RabbitTemplateCustomizer.class);
}

}

@Configuration(proxyBeanMethods = false)
static class ConnectionNameStrategyConfiguration {

Expand Down