From 5f77c1cd77d42fce50253e956d64951f60b68c10 Mon Sep 17 00:00:00 2001 From: dzcr <1137729123@qq.com> Date: Thu, 2 Feb 2023 15:38:54 +0800 Subject: [PATCH] Add RabbitTemplateCustomizer See gh-34050 --- .../amqp/RabbitAutoConfiguration.java | 4 ++- .../amqp/RabbitTemplateCustomizer.java | 36 +++++++++++++++++++ .../amqp/RabbitAutoConfigurationTests.java | 33 +++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitTemplateCustomizer.java 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 f31e8106da..2165ad8180 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 @@ -154,9 +154,11 @@ public class RabbitAutoConfiguration { @Bean @ConditionalOnSingleCandidate(ConnectionFactory.class) @ConditionalOnMissingBean(RabbitOperations.class) - public RabbitTemplate rabbitTemplate(RabbitTemplateConfigurer configurer, ConnectionFactory connectionFactory) { + public RabbitTemplate rabbitTemplate(RabbitTemplateConfigurer configurer, ConnectionFactory connectionFactory, + ObjectProvider rabbitTemplateCustomizers) { RabbitTemplate template = new RabbitTemplate(); configurer.configure(template, connectionFactory); + rabbitTemplateCustomizers.orderedStream().forEach((customizer) -> customizer.customize(template)); return template; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitTemplateCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitTemplateCustomizer.java new file mode 100644 index 0000000000..940e3d5505 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitTemplateCustomizer.java @@ -0,0 +1,36 @@ +/* + * 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}. + * @param rabbitTemplate the rabbitTemplate to customize + */ + void customize(RabbitTemplate rabbitTemplate); + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java index bb3f63adf6..bb98a23cd8 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java @@ -385,6 +385,22 @@ class RabbitAutoConfigurationTests { }); } + @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) -> { @@ -992,6 +1008,23 @@ class RabbitAutoConfigurationTests { } + @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 {