Browse Source

Merge pull request #34050 from dangzhicairang

* pr/34050:
  Polish "Add RabbitTemplateCustomizer"
  Add RabbitTemplateCustomizer

Closes gh-34050
pull/34428/head
Moritz Halbritter 3 years ago
parent
commit
ba8a4c699f
  1. 4
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java
  2. 36
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitTemplateCustomizer.java
  3. 33
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java
  4. 2
      spring-boot-project/spring-boot-docs/src/docs/asciidoc/messaging/amqp.adoc

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

@ -154,9 +154,11 @@ public class RabbitAutoConfiguration { @@ -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<RabbitTemplateCustomizer> customizers) {
RabbitTemplate template = new RabbitTemplate();
configurer.configure(template, connectionFactory);
customizers.orderedStream().forEach((customizer) -> customizer.customize(template));
return template;
}

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

@ -0,0 +1,36 @@ @@ -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 3.1.0
*/
@FunctionalInterface
public interface RabbitTemplateCustomizer {
/**
* Callback to customize a {@link RabbitTemplate} instance.
* @param rabbitTemplate the rabbitTemplate to customize
*/
void customize(RabbitTemplate rabbitTemplate);
}

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

@ -385,6 +385,22 @@ class RabbitAutoConfigurationTests { @@ -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 { @@ -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 {

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

@ -41,6 +41,8 @@ To configure lower-level details of the RabbitMQ `ConnectionFactory` that is use @@ -41,6 +41,8 @@ To configure lower-level details of the RabbitMQ `ConnectionFactory` that is use
If a `ConnectionNameStrategy` bean exists in the context, it will be automatically used to name connections created by the auto-configured `CachingConnectionFactory`.
To make an application-wide, additive customization to the `RabbitTemplate`, use a `RabbitTemplateCustomizer` bean.
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.

Loading…
Cancel
Save