Ensure order is preserved in Rabbit addresses

Using StringUtils.commaDelimitedListToSet() does not preserve order (why?),
so we have to use commaDelimitedListToStringArray().

Fixes gh-1262
pull/1286/head
Dave Syer 10 years ago
parent 8853c7c352
commit 0c52817c88

@ -16,6 +16,9 @@
package org.springframework.boot.autoconfigure.amqp; package org.springframework.boot.autoconfigure.amqp;
import java.util.LinkedHashSet;
import java.util.Set;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -78,8 +81,8 @@ public class RabbitProperties {
} }
private String parseAddresses(String addresses) { private String parseAddresses(String addresses) {
StringBuilder result = new StringBuilder(); Set<String> result = new LinkedHashSet<String>();
for (String address : StringUtils.commaDelimitedListToSet(addresses)) { for (String address : StringUtils.commaDelimitedListToStringArray(addresses)) {
address = address.trim(); address = address.trim();
if (address.startsWith("amqp://")) { if (address.startsWith("amqp://")) {
address = address.substring("amqp://".length()); address = address.substring("amqp://".length());
@ -99,15 +102,12 @@ public class RabbitProperties {
this.virtualHost = address.substring(index + 1); this.virtualHost = address.substring(index + 1);
address = address.substring(0, index); address = address.substring(0, index);
} }
if (result.length() > 0) {
result.append(",");
}
if (!address.contains(":")) { if (!address.contains(":")) {
address = address + ":" + this.port; address = address + ":" + this.port;
} }
result.append(address); result.add(address);
} }
return result.length() > 0 ? result.toString() : null; return result.isEmpty() ? null : StringUtils.collectionToCommaDelimitedString(result);
} }
public void setPort(int port) { public void setPort(int port) {

@ -59,6 +59,13 @@ public class RabbitPropertiesTests {
assertEquals("host", this.properties.getVirtualHost()); assertEquals("host", this.properties.getVirtualHost());
} }
@Test
public void addressesDoubleValuedPreservesOrder() {
this.properties.setAddresses("myhost:9999,ahost:1111/host");
assertNull(this.properties.getHost());
assertEquals("myhost:9999,ahost:1111", properties.getAddresses());
}
@Test @Test
public void addressesSingleValuedWithCredentials() { public void addressesSingleValuedWithCredentials() {
this.properties.setAddresses("amqp://root:password@otherhost:1111/host"); this.properties.setAddresses("amqp://root:password@otherhost:1111/host");

Loading…
Cancel
Save