diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java index dd48820bf983..60e076ca56a3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java @@ -27,6 +27,7 @@ import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.CacheMode; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.ConfirmType; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException; import org.springframework.boot.convert.DurationUnit; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; @@ -43,6 +44,7 @@ * @author Artsiom Yudovin * @author Franjo Zilic * @author EddĂș MelĂ©ndez + * @author Rafael Carvalho * @since 1.0.0 */ @ConfigurationProperties(prefix = "spring.rabbitmq") @@ -203,6 +205,10 @@ public String getAddresses() { */ public String determineAddresses() { if (CollectionUtils.isEmpty(this.parsedAddresses)) { + if (this.host.contains(",")) { + throw new InvalidConfigurationPropertyValueException("spring.rabbitmq.host", this.host, + "Invalid character ','. Value must be a single host. For multiple hosts, use property 'spring.rabbitmq.addresses' instead."); + } return this.host + ":" + determinePort(); } List addressStrings = new ArrayList<>(); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java index 78c36a1e8100..998f5a88d9c6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java @@ -22,8 +22,10 @@ import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; import org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer; import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; +import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * Tests for {@link RabbitProperties}. @@ -31,6 +33,7 @@ * @author Dave Syer * @author Andy Wilkinson * @author Stephane Nicoll + * @author Rafael Carvalho */ class RabbitPropertiesTests { @@ -338,4 +341,13 @@ void determineUsernameWithoutPassword() { assertThat(this.properties.determinePassword()).isEqualTo("guest"); } + @Test + void hostPropertyMustBeSingleHost() { + this.properties.setHost("my-rmq-host.net,my-rmq-host-2.net"); + assertThat(this.properties.getHost()).isEqualTo("my-rmq-host.net,my-rmq-host-2.net"); + assertThatThrownBy(this.properties::determineAddresses) + .isInstanceOf(InvalidConfigurationPropertyValueException.class) + .hasMessageContaining("spring.rabbitmq.host"); + } + }