From 0c52817c884061959e912f7e4a59f7a799c0d7f4 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 17 Jul 2014 12:31:08 +0100 Subject: [PATCH] Ensure order is preserved in Rabbit addresses Using StringUtils.commaDelimitedListToSet() does not preserve order (why?), so we have to use commaDelimitedListToStringArray(). Fixes gh-1262 --- .../boot/autoconfigure/amqp/RabbitProperties.java | 14 +++++++------- .../autoconfigure/amqp/RabbitPropertiesTests.java | 7 +++++++ 2 files changed, 14 insertions(+), 7 deletions(-) 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 ec5af23e74..60647c8e63 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 @@ -16,6 +16,9 @@ package org.springframework.boot.autoconfigure.amqp; +import java.util.LinkedHashSet; +import java.util.Set; + import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.util.StringUtils; @@ -78,8 +81,8 @@ public class RabbitProperties { } private String parseAddresses(String addresses) { - StringBuilder result = new StringBuilder(); - for (String address : StringUtils.commaDelimitedListToSet(addresses)) { + Set result = new LinkedHashSet(); + for (String address : StringUtils.commaDelimitedListToStringArray(addresses)) { address = address.trim(); if (address.startsWith("amqp://")) { address = address.substring("amqp://".length()); @@ -99,15 +102,12 @@ public class RabbitProperties { this.virtualHost = address.substring(index + 1); address = address.substring(0, index); } - if (result.length() > 0) { - result.append(","); - } if (!address.contains(":")) { 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) { 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 index b84bbc2345..5229322d62 100644 --- 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 @@ -59,6 +59,13 @@ public class RabbitPropertiesTests { 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 public void addressesSingleValuedWithCredentials() { this.properties.setAddresses("amqp://root:password@otherhost:1111/host");