diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java index a69bc7077e1..fcd9cbf3b26 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java @@ -98,9 +98,13 @@ public class RabbitAutoConfiguration { @Bean public ConnectionFactory rabbitConnectionFactory(RabbitProperties config) { - CachingConnectionFactory factory = new CachingConnectionFactory( - config.getHost()); - factory.setPort(config.getPort()); + CachingConnectionFactory factory = new CachingConnectionFactory(); + String addresses = config.getAddresses(); + factory.setAddresses(addresses); + if (config.getHost() != null) { + factory.setHost(config.getHost()); + factory.setPort(config.getPort()); + } if (config.getUsername() != null) { factory.setUsername(config.getUsername()); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java index 94ffd5e03be..2d177b96f0b 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java @@ -17,6 +17,7 @@ package org.springframework.boot.autoconfigure.amqp; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.util.StringUtils; /** * Configuration properties for Rabbit. @@ -36,10 +37,19 @@ public class RabbitProperties { private String virtualHost; + private String addresses; + private boolean dynamic = true; public String getHost() { - return this.host; + if (this.addresses == null) { + return this.host; + } + String[] hosts = StringUtils.delimitedListToStringArray(this.addresses, ":"); + if (hosts.length == 2) { + return hosts[0]; + } + return null; } public void setHost(String host) { @@ -47,9 +57,25 @@ public class RabbitProperties { } public int getPort() { + if (this.addresses == null) { + return this.port; + } + String[] hosts = StringUtils.delimitedListToStringArray(this.addresses, ":"); + if (hosts.length >= 2) { + return Integer + .valueOf(StringUtils.commaDelimitedListToStringArray(hosts[1])[0]); + } return this.port; } + public String getAddresses() { + return this.addresses == null ? this.host + ":" + this.port : this.addresses; + } + + public void setAddresses(String addresses) { + this.addresses = addresses; + } + public void setPort(int port) { this.port = port; } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java new file mode 100644 index 00000000000..2c2adc2ce77 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java @@ -0,0 +1,50 @@ +/* + * Copyright 2012-2013 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 + * + * http://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.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * @author Dave Syer + */ +public class RabbitPropertiesTests { + + private RabbitProperties properties = new RabbitProperties(); + + @Test + public void addressesNotSet() { + assertEquals("localhost", this.properties.getHost()); + assertEquals(5672, this.properties.getPort()); + } + + @Test + public void addressesSingleValued() { + this.properties.setAddresses("myhost:9999"); + assertEquals("myhost", this.properties.getHost()); + assertEquals(9999, this.properties.getPort()); + } + + @Test + public void addressesDoubleValued() { + this.properties.setAddresses("myhost:9999,otherhost:1111"); + assertEquals(null, this.properties.getHost()); + assertEquals(9999, this.properties.getPort()); + } + +}