Commit 0fedb24c authored by Mark Pollack's avatar Mark Pollack Committed by Stephane Nicoll

Support amqps:// URIs in spring.rabbitmq.addresses

See gh-18808
Co-Authored-By: 's avatarBryan Kelly <xyloman@gmail.com>
parent ed50bf24
...@@ -937,6 +937,10 @@ public class RabbitProperties { ...@@ -937,6 +937,10 @@ public class RabbitProperties {
private static final int DEFAULT_PORT = 5672; private static final int DEFAULT_PORT = 5672;
private static final String PREFIX_AMQP_SECURE = "amqps://";
private static final int DEFAULT_PORT_SECURE = 5671;
private String host; private String host;
private int port; private int port;
...@@ -947,6 +951,8 @@ public class RabbitProperties { ...@@ -947,6 +951,8 @@ public class RabbitProperties {
private String virtualHost; private String virtualHost;
private boolean isSecureConnection;
private Address(String input) { private Address(String input) {
input = input.trim(); input = input.trim();
input = trimPrefix(input); input = trimPrefix(input);
...@@ -956,6 +962,10 @@ public class RabbitProperties { ...@@ -956,6 +962,10 @@ public class RabbitProperties {
} }
private String trimPrefix(String input) { private String trimPrefix(String input) {
if (input.startsWith(PREFIX_AMQP_SECURE)) {
this.isSecureConnection = true;
return input.substring(PREFIX_AMQP_SECURE.length());
}
if (input.startsWith(PREFIX_AMQP)) { if (input.startsWith(PREFIX_AMQP)) {
input = input.substring(PREFIX_AMQP.length()); input = input.substring(PREFIX_AMQP.length());
} }
...@@ -992,7 +1002,12 @@ public class RabbitProperties { ...@@ -992,7 +1002,12 @@ public class RabbitProperties {
int portIndex = input.indexOf(':'); int portIndex = input.indexOf(':');
if (portIndex == -1) { if (portIndex == -1) {
this.host = input; this.host = input;
this.port = DEFAULT_PORT; if (this.isSecureConnection) {
this.port = DEFAULT_PORT_SECURE;
}
else {
this.port = DEFAULT_PORT;
}
} }
else { else {
this.host = input.substring(0, portIndex); this.host = input.substring(0, portIndex);
......
...@@ -70,6 +70,13 @@ public class RabbitPropertiesTests { ...@@ -70,6 +70,13 @@ public class RabbitPropertiesTests {
assertThat(this.properties.getPort()).isEqualTo(1234); assertThat(this.properties.getPort()).isEqualTo(1234);
} }
@Test
void usingSecuredConnections() {
this.properties.setAddresses("amqps://root:password@otherhost,amqps://root:password2@otherhost2");
assertThat(this.properties.determinePort()).isEqualTo(5671);
assertThat(this.properties.determineAddresses()).isEqualTo("otherhost:5671,otherhost2:5671");
}
@Test @Test
public void determinePortReturnsPortOfFirstAddress() { public void determinePortReturnsPortOfFirstAddress() {
this.properties.setAddresses("rabbit1.example.com:1234,rabbit2.example.com:2345"); this.properties.setAddresses("rabbit1.example.com:1234,rabbit2.example.com:2345");
......
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