Support for setting credentials and vhost in rabbit addresses

User can now add credentials, vhost and protocol prefix (amqp://)
to any or all of the addresses, extending the format beyond that accepted
bu the rabbitmq client, but making it cloud friendly. Only one of
the addresses needs those properties and all are optional. Port
also defaults to 5672 in an address.
This commit is contained in:
Dave Syer 2014-07-07 17:00:57 +01:00
parent 148e32d09a
commit aa38d33404
2 changed files with 62 additions and 1 deletions

View File

@ -70,13 +70,46 @@ public class RabbitProperties {
}
public void setAddresses(String addresses) {
this.addresses = addresses;
this.addresses = parseAddresses(addresses);
}
public String getAddresses() {
return (this.addresses == null ? this.host + ":" + this.port : this.addresses);
}
private String parseAddresses(String addresses) {
StringBuilder result = new StringBuilder();
for (String address : StringUtils.commaDelimitedListToSet(addresses)) {
address = address.trim();
if (address.startsWith("amqp://")) {
address = address.substring("amqp://".length());
}
if (address.contains("@")) {
String[] split = StringUtils.split(address, "@");
String creds = split[0];
address = split[1];
split = StringUtils.split(creds, ":");
this.username = split[0];
if (split.length > 0) {
this.password = split[1];
}
}
int index = address.indexOf("/");
if (index >= 0 && index < address.length()) {
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);
}
return result.length() > 0 ? result.toString() : null;
}
public void setPort(int port) {
this.port = port;
}

View File

@ -50,6 +50,34 @@ public class RabbitPropertiesTests {
assertEquals(9999, this.properties.getPort());
}
@Test
public void addressesDoubleValuedWithCredentials() {
this.properties.setAddresses("myhost:9999,root:password@otherhost:1111/host");
assertNull(this.properties.getHost());
assertEquals(9999, this.properties.getPort());
assertEquals("root", this.properties.getUsername());
assertEquals("host", this.properties.getVirtualHost());
}
@Test
public void addressesSingleValuedWithCredentials() {
this.properties.setAddresses("amqp://root:password@otherhost:1111/host");
assertEquals("otherhost", this.properties.getHost());
assertEquals(1111, this.properties.getPort());
assertEquals("root", this.properties.getUsername());
assertEquals("host", this.properties.getVirtualHost());
}
@Test
public void addressesSingleValuedWithCredentialsDefaultPort() {
this.properties.setAddresses("amqp://root:password@lemur.cloudamqp.com/host");
assertEquals("lemur.cloudamqp.com", this.properties.getHost());
assertEquals(5672, this.properties.getPort());
assertEquals("root", this.properties.getUsername());
assertEquals("host", this.properties.getVirtualHost());
assertEquals("lemur.cloudamqp.com:5672", this.properties.getAddresses());
}
@Test
public void testDefaultVirtualHost() {
this.properties.setVirtualHost("/");