Commit 2e25a256 authored by Andy Wilkinson's avatar Andy Wilkinson

Allow the auto-configured RabbitMQ ConnectionFactory to be customized

Closes gh-6719
parent a481b4ae
/*
* 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);
}
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -100,9 +100,13 @@ public class RabbitAutoConfiguration { ...@@ -100,9 +100,13 @@ public class RabbitAutoConfiguration {
public CachingConnectionFactory rabbitConnectionFactory(RabbitProperties properties, public CachingConnectionFactory rabbitConnectionFactory(RabbitProperties properties,
ResourceLoader resourceLoader, ObjectProvider<CredentialsProvider> credentialsProvider, ResourceLoader resourceLoader, ObjectProvider<CredentialsProvider> credentialsProvider,
ObjectProvider<CredentialsRefreshService> credentialsRefreshService, ObjectProvider<CredentialsRefreshService> credentialsRefreshService,
ObjectProvider<ConnectionNameStrategy> connectionNameStrategy) throws Exception { ObjectProvider<ConnectionNameStrategy> connectionNameStrategy,
CachingConnectionFactory factory = new CachingConnectionFactory(getRabbitConnectionFactoryBean(properties, ObjectProvider<ConnectionFactoryCustomizer> connectionFactoryCustomizers) throws Exception {
resourceLoader, credentialsProvider, credentialsRefreshService).getObject()); 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(); PropertyMapper map = PropertyMapper.get();
map.from(properties::determineAddresses).to(factory::setAddresses); map.from(properties::determineAddresses).to(factory::setAddresses);
map.from(properties::getAddressShuffleMode).whenNonNull().to(factory::setAddressShuffleMode); map.from(properties::getAddressShuffleMode).whenNonNull().to(factory::setAddressShuffleMode);
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -26,6 +26,7 @@ import javax.net.ssl.TrustManager; ...@@ -26,6 +26,7 @@ import javax.net.ssl.TrustManager;
import com.rabbitmq.client.Address; import com.rabbitmq.client.Address;
import com.rabbitmq.client.Connection; import com.rabbitmq.client.Connection;
import com.rabbitmq.client.JDKSaslConfig;
import com.rabbitmq.client.SslContextFactory; import com.rabbitmq.client.SslContextFactory;
import com.rabbitmq.client.TrustEverythingTrustManager; import com.rabbitmq.client.TrustEverythingTrustManager;
import com.rabbitmq.client.impl.CredentialsProvider; import com.rabbitmq.client.impl.CredentialsProvider;
...@@ -33,6 +34,7 @@ import com.rabbitmq.client.impl.CredentialsRefreshService; ...@@ -33,6 +34,7 @@ import com.rabbitmq.client.impl.CredentialsRefreshService;
import com.rabbitmq.client.impl.DefaultCredentialsProvider; import com.rabbitmq.client.impl.DefaultCredentialsProvider;
import org.aopalliance.aop.Advice; import org.aopalliance.aop.Advice;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.InOrder;
import org.springframework.amqp.core.AcknowledgeMode; import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.core.AmqpAdmin;
...@@ -60,6 +62,8 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner; ...@@ -60,6 +62,8 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; 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.RetryPolicy;
import org.springframework.retry.backoff.BackOffPolicy; import org.springframework.retry.backoff.BackOffPolicy;
import org.springframework.retry.backoff.ExponentialBackOffPolicy; import org.springframework.retry.backoff.ExponentialBackOffPolicy;
...@@ -75,6 +79,7 @@ import static org.mockito.ArgumentMatchers.anyString; ...@@ -75,6 +79,7 @@ import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
...@@ -822,6 +827,29 @@ class RabbitAutoConfigurationTests { ...@@ -822,6 +827,29 @@ class RabbitAutoConfigurationTests {
.isNull()); .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) { private TrustManager getTrustManager(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory) {
SslContextFactory sslContextFactory = (SslContextFactory) ReflectionTestUtils.getField(rabbitConnectionFactory, SslContextFactory sslContextFactory = (SslContextFactory) ReflectionTestUtils.getField(rabbitConnectionFactory,
"sslContextFactory"); "sslContextFactory");
...@@ -1071,4 +1099,31 @@ class RabbitAutoConfigurationTests { ...@@ -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);
}
}
} }
...@@ -5428,8 +5428,10 @@ Alternatively, you could configure the same connection using the `addresses` att ...@@ -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. 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 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 property-based configuration options.
See {spring-boot-autoconfigure-module-code}/amqp/RabbitProperties.java[`RabbitProperties`] for more of the supported 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. 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.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment