From 2e25a256d57c7c11753c3668e12fe5abc9759343 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 13 Apr 2021 17:18:39 +0100 Subject: [PATCH] Allow the auto-configured RabbitMQ ConnectionFactory to be customized Closes gh-6719 --- .../amqp/ConnectionFactoryCustomizer.java | 37 ++++++++++++ .../amqp/RabbitAutoConfiguration.java | 12 ++-- .../amqp/RabbitAutoConfigurationTests.java | 57 ++++++++++++++++++- .../docs/asciidoc/spring-boot-features.adoc | 6 +- 4 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/ConnectionFactoryCustomizer.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/ConnectionFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/ConnectionFactoryCustomizer.java new file mode 100644 index 0000000000..164b121bdc --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/ConnectionFactoryCustomizer.java @@ -0,0 +1,37 @@ +/* + * Copyright 2012-2021 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 com.rabbitmq.client.ConnectionFactory; + +/** + * Callback interface that can be implemented by beans wishing to customize the + * auto-configured RabbitMQ {@link ConnectionFactory}. + * + * @author Andy Wilkinson + * @since 2.5.0 + */ +@FunctionalInterface +public interface ConnectionFactoryCustomizer { + + /** + * Customize the {@link ConnectionFactory}. + * @param factory the factory to customize + */ + void customize(ConnectionFactory factory); + +} 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 91ee1e6acf..3c09a5b2fd 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 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. @@ -100,9 +100,13 @@ public class RabbitAutoConfiguration { public CachingConnectionFactory rabbitConnectionFactory(RabbitProperties properties, ResourceLoader resourceLoader, ObjectProvider credentialsProvider, ObjectProvider credentialsRefreshService, - ObjectProvider connectionNameStrategy) throws Exception { - CachingConnectionFactory factory = new CachingConnectionFactory(getRabbitConnectionFactoryBean(properties, - resourceLoader, credentialsProvider, credentialsRefreshService).getObject()); + ObjectProvider connectionNameStrategy, + ObjectProvider connectionFactoryCustomizers) throws Exception { + com.rabbitmq.client.ConnectionFactory connectionFactory = getRabbitConnectionFactoryBean(properties, + resourceLoader, credentialsProvider, credentialsRefreshService).getObject(); + connectionFactoryCustomizers.orderedStream() + .forEach((customizer) -> customizer.customize(connectionFactory)); + CachingConnectionFactory factory = new CachingConnectionFactory(connectionFactory); PropertyMapper map = PropertyMapper.get(); map.from(properties::determineAddresses).to(factory::setAddresses); map.from(properties::getAddressShuffleMode).whenNonNull().to(factory::setAddressShuffleMode); 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 4e1e23dfff..32ea8ee0f6 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 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. @@ -26,6 +26,7 @@ import javax.net.ssl.TrustManager; import com.rabbitmq.client.Address; import com.rabbitmq.client.Connection; +import com.rabbitmq.client.JDKSaslConfig; import com.rabbitmq.client.SslContextFactory; import com.rabbitmq.client.TrustEverythingTrustManager; import com.rabbitmq.client.impl.CredentialsProvider; @@ -33,6 +34,7 @@ import com.rabbitmq.client.impl.CredentialsRefreshService; import com.rabbitmq.client.impl.DefaultCredentialsProvider; import org.aopalliance.aop.Advice; import org.junit.jupiter.api.Test; +import org.mockito.InOrder; import org.springframework.amqp.core.AcknowledgeMode; import org.springframework.amqp.core.AmqpAdmin; @@ -60,6 +62,8 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.retry.RetryPolicy; import org.springframework.retry.backoff.BackOffPolicy; import org.springframework.retry.backoff.ExponentialBackOffPolicy; @@ -75,6 +79,7 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -822,6 +827,29 @@ class RabbitAutoConfigurationTests { .isNull()); } + @Test + void whenAConnectionFactoryCustomizerIsDefinedThenItCustomizesTheConnectionFactory() { + this.contextRunner.withUserConfiguration(SaslConfigCustomizerConfiguration.class) + .run((context) -> assertThat(getTargetConnectionFactory(context).getSaslConfig()) + .isInstanceOf(JDKSaslConfig.class)); + } + + @Test + void whenMultipleConnectionFactoryCustomizersAreDefinedThenTheyAreCalledInOrder() { + this.contextRunner.withUserConfiguration(MultipleConnectionFactoryCustomizersConfiguration.class) + .run((context) -> { + ConnectionFactoryCustomizer firstCustomizer = context.getBean("firstCustomizer", + ConnectionFactoryCustomizer.class); + ConnectionFactoryCustomizer secondCustomizer = context.getBean("secondCustomizer", + ConnectionFactoryCustomizer.class); + InOrder inOrder = inOrder(firstCustomizer, secondCustomizer); + com.rabbitmq.client.ConnectionFactory targetConnectionFactory = getTargetConnectionFactory(context); + inOrder.verify(firstCustomizer).customize(targetConnectionFactory); + inOrder.verify(secondCustomizer).customize(targetConnectionFactory); + inOrder.verifyNoMoreInteractions(); + }); + } + private TrustManager getTrustManager(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory) { SslContextFactory sslContextFactory = (SslContextFactory) ReflectionTestUtils.getField(rabbitConnectionFactory, "sslContextFactory"); @@ -1071,4 +1099,31 @@ class RabbitAutoConfigurationTests { } + @Configuration(proxyBeanMethods = false) + static class SaslConfigCustomizerConfiguration { + + @Bean + ConnectionFactoryCustomizer connectionFactoryCustomizer() { + return (connectionFactory) -> connectionFactory.setSaslConfig(new JDKSaslConfig(connectionFactory)); + } + + } + + @Configuration(proxyBeanMethods = false) + static class MultipleConnectionFactoryCustomizersConfiguration { + + @Bean + @Order(Ordered.LOWEST_PRECEDENCE) + ConnectionFactoryCustomizer secondCustomizer() { + return mock(ConnectionFactoryCustomizer.class); + } + + @Bean + @Order(0) + ConnectionFactoryCustomizer firstCustomizer() { + return mock(ConnectionFactoryCustomizer.class); + } + + } + } diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc index e01d927024..da9ba523c6 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc @@ -5428,8 +5428,10 @@ Alternatively, you could configure the same connection using the `addresses` att NOTE: When specifying addresses that way, the `host` and `port` properties are ignored. If the address uses the `amqps` protocol, SSL support is enabled automatically. -If a `ConnectionNameStrategy` bean exists in the context, it will be automatically used to name connections created by the auto-configured `ConnectionFactory`. -See {spring-boot-autoconfigure-module-code}/amqp/RabbitProperties.java[`RabbitProperties`] for more of the supported options. +See {spring-boot-autoconfigure-module-code}/amqp/RabbitProperties.java[`RabbitProperties`] for more of the supported property-based configuration options. +To configure lower-level details of the RabbitMQ `ConnectionFactory` that is used by Spring AMQP, define a `ConnectionFactoryCustomizer` bean. + +If a `ConnectionNameStrategy` bean exists in the context, it will be automatically used to name connections created by the auto-configured `CachingConnectionFactory`. 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.