Skip to content

Commit 4d1373c

Browse files
committed
Polish "Support amqps:// URIs in spring.rabbitmq.addresses"
See gh-18808
1 parent 0fedb24 commit 4d1373c

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private RabbitConnectionFactoryBean getRabbitConnectionFactoryBean(RabbitPropert
125125
map.from(properties::getRequestedHeartbeat).whenNonNull().asInt(Duration::getSeconds)
126126
.to(factory::setRequestedHeartbeat);
127127
RabbitProperties.Ssl ssl = properties.getSsl();
128-
if (ssl.isEnabled()) {
128+
if (ssl.determineEnabled()) {
129129
factory.setUseSSL(true);
130130
map.from(ssl::getAlgorithm).whenNonNull().to(factory::setSslAlgorithm);
131131
map.from(ssl::getKeyStoreType).to(factory::setKeyStoreType);

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public Template getTemplate() {
310310
return this.template;
311311
}
312312

313-
public static class Ssl {
313+
public class Ssl {
314314

315315
/**
316316
* Whether to enable SSL support.
@@ -366,6 +366,21 @@ public boolean isEnabled() {
366366
return this.enabled;
367367
}
368368

369+
/**
370+
* Returns whether SSL is enabled from the first address, or the configured ssl
371+
* enabled flag if no addresses have been set.
372+
* @return whether ssl is enabled
373+
* @see #setAddresses(String)
374+
* @see #isEnabled()
375+
*/
376+
public boolean determineEnabled() {
377+
if (CollectionUtils.isEmpty(RabbitProperties.this.parsedAddresses)) {
378+
return isEnabled();
379+
}
380+
Address address = RabbitProperties.this.parsedAddresses.get(0);
381+
return address.secureConnection;
382+
}
383+
369384
public void setEnabled(boolean enabled) {
370385
this.enabled = enabled;
371386
}
@@ -951,7 +966,7 @@ private static final class Address {
951966

952967
private String virtualHost;
953968

954-
private boolean isSecureConnection;
969+
private boolean secureConnection;
955970

956971
private Address(String input) {
957972
input = input.trim();
@@ -963,7 +978,7 @@ private Address(String input) {
963978

964979
private String trimPrefix(String input) {
965980
if (input.startsWith(PREFIX_AMQP_SECURE)) {
966-
this.isSecureConnection = true;
981+
this.secureConnection = true;
967982
return input.substring(PREFIX_AMQP_SECURE.length());
968983
}
969984
if (input.startsWith(PREFIX_AMQP)) {
@@ -1002,12 +1017,7 @@ private void parseHostAndPort(String input) {
10021017
int portIndex = input.indexOf(':');
10031018
if (portIndex == -1) {
10041019
this.host = input;
1005-
if (this.isSecureConnection) {
1006-
this.port = DEFAULT_PORT_SECURE;
1007-
}
1008-
else {
1009-
this.port = DEFAULT_PORT;
1010-
}
1020+
this.port = (this.secureConnection) ? DEFAULT_PORT_SECURE : DEFAULT_PORT;
10111021
}
10121022
else {
10131023
this.host = input.substring(0, portIndex);

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,6 @@ public void customPort() {
7070
assertThat(this.properties.getPort()).isEqualTo(1234);
7171
}
7272

73-
@Test
74-
void usingSecuredConnections() {
75-
this.properties.setAddresses("amqps://root:password@otherhost,amqps://root:password2@otherhost2");
76-
assertThat(this.properties.determinePort()).isEqualTo(5671);
77-
assertThat(this.properties.determineAddresses()).isEqualTo("otherhost:5671,otherhost2:5671");
78-
}
79-
8073
@Test
8174
public void determinePortReturnsPortOfFirstAddress() {
8275
this.properties.setAddresses("rabbit1.example.com:1234,rabbit2.example.com:2345");
@@ -96,6 +89,18 @@ public void determinePortReturnsDefaultAmqpPortWhenFirstAddressHasNoExplicitPort
9689
assertThat(this.properties.determinePort()).isEqualTo(5672);
9790
}
9891

92+
@Test
93+
public void determinePortUsingAmqpReturnsPortOfFirstAddress() {
94+
this.properties.setAddresses("amqp://root:password@otherhost,amqps://root:password2@otherhost2");
95+
assertThat(this.properties.determinePort()).isEqualTo(5672);
96+
}
97+
98+
@Test
99+
public void determinePortUsingAmqpsReturnsPortOfFirstAddress() {
100+
this.properties.setAddresses("amqps://root:password@otherhost,amqp://root:password2@otherhost2");
101+
assertThat(this.properties.determinePort()).isEqualTo(5671);
102+
}
103+
99104
@Test
100105
public void virtualHostDefaultsToNull() {
101106
assertThat(this.properties.getVirtualHost()).isNull();
@@ -229,6 +234,24 @@ public void determineAddressesUsesHostAndPortPropertiesWhenNoAddressesSet() {
229234
assertThat(this.properties.determineAddresses()).isEqualTo("rabbit.example.com:1234");
230235
}
231236

237+
@Test
238+
public void determineSslUsingAmqpsReturnsStateOfFirstAddress() {
239+
this.properties.setAddresses("amqps://root:password@otherhost,amqp://root:password2@otherhost2");
240+
assertThat(this.properties.getSsl().determineEnabled()).isTrue();
241+
}
242+
243+
@Test
244+
public void determineSslUsingAmqpReturnsStateOfFirstAddress() {
245+
this.properties.setAddresses("amqp://root:password@otherhost,amqps://root:password2@otherhost2");
246+
assertThat(this.properties.getSsl().determineEnabled()).isFalse();
247+
}
248+
249+
@Test
250+
public void determineSslReturnFlagPropertyWhenNoAddresses() {
251+
this.properties.getSsl().setEnabled(true);
252+
assertThat(this.properties.getSsl().determineEnabled()).isTrue();
253+
}
254+
232255
@Test
233256
public void simpleContainerUseConsistentDefaultValues() {
234257
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4762,6 +4762,13 @@ For example, you might declare the following section in `application.properties`
47624762
spring.rabbitmq.password=secret
47634763
----
47644764

4765+
Alternatively, you could configure the same connection using the `addresses` attributes:
4766+
4767+
[source,properties,indent=0]
4768+
----
4769+
spring.rabbitmq.addresses=amqp://admin:secret@localhost
4770+
----
4771+
47654772
If a `ConnectionNameStrategy` bean exists in the context, it will be automatically used to name connections created by the auto-configured `ConnectionFactory`.
47664773
See {spring-boot-autoconfigure-module-code}/amqp/RabbitProperties.java[`RabbitProperties`] for more of the supported options.
47674774

0 commit comments

Comments
 (0)