Commit 01137b6f authored by Dave Syer's avatar Dave Syer

Add "addresses" to RabbitProperties

If user sets addresses it supercedes anything that was set in host
or port (same as in the native ConnectionFactory).

Fixes gh-524
parent cb52cb55
...@@ -98,9 +98,13 @@ public class RabbitAutoConfiguration { ...@@ -98,9 +98,13 @@ public class RabbitAutoConfiguration {
@Bean @Bean
public ConnectionFactory rabbitConnectionFactory(RabbitProperties config) { public ConnectionFactory rabbitConnectionFactory(RabbitProperties config) {
CachingConnectionFactory factory = new CachingConnectionFactory( CachingConnectionFactory factory = new CachingConnectionFactory();
config.getHost()); String addresses = config.getAddresses();
factory.setPort(config.getPort()); factory.setAddresses(addresses);
if (config.getHost() != null) {
factory.setHost(config.getHost());
factory.setPort(config.getPort());
}
if (config.getUsername() != null) { if (config.getUsername() != null) {
factory.setUsername(config.getUsername()); factory.setUsername(config.getUsername());
} }
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.amqp; package org.springframework.boot.autoconfigure.amqp;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils;
/** /**
* Configuration properties for Rabbit. * Configuration properties for Rabbit.
...@@ -36,10 +37,19 @@ public class RabbitProperties { ...@@ -36,10 +37,19 @@ public class RabbitProperties {
private String virtualHost; private String virtualHost;
private String addresses;
private boolean dynamic = true; private boolean dynamic = true;
public String getHost() { 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) { public void setHost(String host) {
...@@ -47,9 +57,25 @@ public class RabbitProperties { ...@@ -47,9 +57,25 @@ public class RabbitProperties {
} }
public int getPort() { 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; 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) { public void setPort(int port) {
this.port = port; this.port = port;
} }
......
/*
* 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());
}
}
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