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
This commit is contained in:
Dave Syer 2014-03-18 17:56:24 +00:00
parent cb52cb5555
commit 01137b6fd6
3 changed files with 84 additions and 4 deletions

View File

@ -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());
}

View File

@ -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;
}

View File

@ -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());
}
}