Allow the auto-configured RabbitMQ ConnectionFactory to be customized
Closes gh-6719
This commit is contained in:
@@ -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);
|
||||
|
||||
}
|
||||
@@ -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> credentialsProvider,
|
||||
ObjectProvider<CredentialsRefreshService> credentialsRefreshService,
|
||||
ObjectProvider<ConnectionNameStrategy> connectionNameStrategy) throws Exception {
|
||||
CachingConnectionFactory factory = new CachingConnectionFactory(getRabbitConnectionFactoryBean(properties,
|
||||
resourceLoader, credentialsProvider, credentialsRefreshService).getObject());
|
||||
ObjectProvider<ConnectionNameStrategy> connectionNameStrategy,
|
||||
ObjectProvider<ConnectionFactoryCustomizer> 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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user