Skip to content

Commit ba8a4c6

Browse files
committed
Merge pull request #34050 from dangzhicairang
* pr/34050: Polish "Add RabbitTemplateCustomizer" Add RabbitTemplateCustomizer Closes gh-34050
2 parents 9448623 + 2becf70 commit ba8a4c6

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,11 @@ public RabbitTemplateConfigurer rabbitTemplateConfigurer(RabbitProperties proper
154154
@Bean
155155
@ConditionalOnSingleCandidate(ConnectionFactory.class)
156156
@ConditionalOnMissingBean(RabbitOperations.class)
157-
public RabbitTemplate rabbitTemplate(RabbitTemplateConfigurer configurer, ConnectionFactory connectionFactory) {
157+
public RabbitTemplate rabbitTemplate(RabbitTemplateConfigurer configurer, ConnectionFactory connectionFactory,
158+
ObjectProvider<RabbitTemplateCustomizer> customizers) {
158159
RabbitTemplate template = new RabbitTemplate();
159160
configurer.configure(template, connectionFactory);
161+
customizers.orderedStream().forEach((customizer) -> customizer.customize(template));
160162
return template;
161163
}
162164

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.amqp;
18+
19+
import org.springframework.amqp.rabbit.core.RabbitTemplate;
20+
21+
/**
22+
* Callback interface that can be used to customize a {@link RabbitTemplate}.
23+
*
24+
* @author dang zhicairang
25+
* @since 3.1.0
26+
*/
27+
@FunctionalInterface
28+
public interface RabbitTemplateCustomizer {
29+
30+
/**
31+
* Callback to customize a {@link RabbitTemplate} instance.
32+
* @param rabbitTemplate the rabbitTemplate to customize
33+
*/
34+
void customize(RabbitTemplate rabbitTemplate);
35+
36+
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,22 @@ void testRabbitTemplateConfigurerUsesConfig() {
385385
});
386386
}
387387

388+
@Test
389+
void whenMultipleRabbitTemplateCustomizersAreDefinedThenTheyAreCalledInOrder() {
390+
this.contextRunner.withUserConfiguration(MultipleRabbitTemplateCustomizersConfiguration.class)
391+
.run((context) -> {
392+
RabbitTemplateCustomizer firstCustomizer = context.getBean("firstCustomizer",
393+
RabbitTemplateCustomizer.class);
394+
RabbitTemplateCustomizer secondCustomizer = context.getBean("secondCustomizer",
395+
RabbitTemplateCustomizer.class);
396+
InOrder inOrder = inOrder(firstCustomizer, secondCustomizer);
397+
RabbitTemplate template = context.getBean(RabbitTemplate.class);
398+
then(firstCustomizer).should(inOrder).customize(template);
399+
then(secondCustomizer).should(inOrder).customize(template);
400+
inOrder.verifyNoMoreInteractions();
401+
});
402+
}
403+
388404
@Test
389405
void testConnectionFactoryBackOff() {
390406
this.contextRunner.withUserConfiguration(TestConfiguration2.class).run((context) -> {
@@ -992,6 +1008,23 @@ MessageRecoverer anotherMessageRecoverer() {
9921008

9931009
}
9941010

1011+
@Configuration(proxyBeanMethods = false)
1012+
static class MultipleRabbitTemplateCustomizersConfiguration {
1013+
1014+
@Bean
1015+
@Order(Ordered.LOWEST_PRECEDENCE)
1016+
RabbitTemplateCustomizer secondCustomizer() {
1017+
return mock(RabbitTemplateCustomizer.class);
1018+
}
1019+
1020+
@Bean
1021+
@Order(0)
1022+
RabbitTemplateCustomizer firstCustomizer() {
1023+
return mock(RabbitTemplateCustomizer.class);
1024+
}
1025+
1026+
}
1027+
9951028
@Configuration(proxyBeanMethods = false)
9961029
static class ConnectionNameStrategyConfiguration {
9971030

spring-boot-project/spring-boot-docs/src/docs/asciidoc/messaging/amqp.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ To configure lower-level details of the RabbitMQ `ConnectionFactory` that is use
4141

4242
If a `ConnectionNameStrategy` bean exists in the context, it will be automatically used to name connections created by the auto-configured `CachingConnectionFactory`.
4343

44+
To make an application-wide, additive customization to the `RabbitTemplate`, use a `RabbitTemplateCustomizer` bean.
45+
4446
TIP: See https://spring.io/blog/2010/06/14/understanding-amqp-the-protocol-used-by-rabbitmq/[Understanding AMQP, the protocol used by RabbitMQ] for more details.
4547

4648

0 commit comments

Comments
 (0)