Skip to content

Commit 722d374

Browse files
committed
Rationalize RabbitProperties
Closes gh-18830
1 parent ef592ea commit 722d374

File tree

3 files changed

+57
-24
lines changed

3 files changed

+57
-24
lines changed

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

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.time.temporal.ChronoUnit;
2121
import java.util.ArrayList;
2222
import java.util.List;
23+
import java.util.Optional;
2324

2425
import org.springframework.amqp.core.AcknowledgeMode;
2526
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.CacheMode;
@@ -45,15 +46,20 @@
4546
@ConfigurationProperties(prefix = "spring.rabbitmq")
4647
public class RabbitProperties {
4748

49+
private static final int DEFAULT_PORT = 5672;
50+
51+
private static final int DEFAULT_PORT_SECURE = 5671;
52+
4853
/**
49-
* RabbitMQ host.
54+
* RabbitMQ host. Ignored if an address is set.
5055
*/
5156
private String host = "localhost";
5257

5358
/**
54-
* RabbitMQ port.
59+
* RabbitMQ port. Ignored if an address is set. Default to 5672, or 5671 if SSL is
60+
* enabled.
5561
*/
56-
private int port = 5672;
62+
private Integer port;
5763

5864
/**
5965
* Login user to authenticate to the broker.
@@ -76,7 +82,8 @@ public class RabbitProperties {
7682
private String virtualHost;
7783

7884
/**
79-
* Comma-separated list of addresses to which the client should connect.
85+
* Comma-separated list of addresses to which the client should connect. When set, the
86+
* host and port are ignored.
8087
*/
8188
private String addresses;
8289

@@ -143,7 +150,7 @@ public void setHost(String host) {
143150
this.host = host;
144151
}
145152

146-
public int getPort() {
153+
public Integer getPort() {
147154
return this.port;
148155
}
149156

@@ -156,13 +163,16 @@ public int getPort() {
156163
*/
157164
public int determinePort() {
158165
if (CollectionUtils.isEmpty(this.parsedAddresses)) {
159-
return getPort();
166+
Integer port = getPort();
167+
if (port != null) {
168+
return port;
169+
}
170+
return (Optional.ofNullable(getSsl().getEnabled()).orElse(false)) ? DEFAULT_PORT_SECURE : DEFAULT_PORT;
160171
}
161-
Address address = this.parsedAddresses.get(0);
162-
return address.port;
172+
return this.parsedAddresses.get(0).port;
163173
}
164174

165-
public void setPort(int port) {
175+
public void setPort(Integer port) {
166176
this.port = port;
167177
}
168178

@@ -177,7 +187,7 @@ public String getAddresses() {
177187
*/
178188
public String determineAddresses() {
179189
if (CollectionUtils.isEmpty(this.parsedAddresses)) {
180-
return this.host + ":" + this.port;
190+
return this.host + ":" + determinePort();
181191
}
182192
List<String> addressStrings = new ArrayList<>();
183193
for (Address parsedAddress : this.parsedAddresses) {
@@ -194,7 +204,7 @@ public void setAddresses(String addresses) {
194204
private List<Address> parseAddresses(String addresses) {
195205
List<Address> parsedAddresses = new ArrayList<>();
196206
for (String address : StringUtils.commaDelimitedListToStringArray(addresses)) {
197-
parsedAddresses.add(new Address(address, getSsl().isEnabled()));
207+
parsedAddresses.add(new Address(address, Optional.ofNullable(getSsl().getEnabled()).orElse(false)));
198208
}
199209
return parsedAddresses;
200210
}
@@ -327,9 +337,10 @@ public Template getTemplate() {
327337
public class Ssl {
328338

329339
/**
330-
* Whether to enable SSL support.
340+
* Whether to enable SSL support. Determined automatically if an address is
341+
* provided with the protocol (amqp:// vs. amqps://).
331342
*/
332-
private boolean enabled;
343+
private Boolean enabled;
333344

334345
/**
335346
* Path to the key store that holds the SSL certificate.
@@ -376,7 +387,7 @@ public class Ssl {
376387
*/
377388
private boolean verifyHostname = true;
378389

379-
public boolean isEnabled() {
390+
public Boolean getEnabled() {
380391
return this.enabled;
381392
}
382393

@@ -385,17 +396,18 @@ public boolean isEnabled() {
385396
* enabled flag if no addresses have been set.
386397
* @return whether ssl is enabled
387398
* @see #setAddresses(String)
388-
* @see #isEnabled()
399+
* @see #getEnabled() ()
389400
*/
390401
public boolean determineEnabled() {
402+
boolean defaultEnabled = Optional.ofNullable(getEnabled()).orElse(false);
391403
if (CollectionUtils.isEmpty(RabbitProperties.this.parsedAddresses)) {
392-
return isEnabled();
404+
return defaultEnabled;
393405
}
394406
Address address = RabbitProperties.this.parsedAddresses.get(0);
395-
return address.determineSslEnabled(isEnabled());
407+
return address.determineSslEnabled(defaultEnabled);
396408
}
397409

398-
public void setEnabled(boolean enabled) {
410+
public void setEnabled(Boolean enabled) {
399411
this.enabled = enabled;
400412
}
401413

@@ -953,12 +965,8 @@ private static final class Address {
953965

954966
private static final String PREFIX_AMQP = "amqp://";
955967

956-
private static final int DEFAULT_PORT = 5672;
957-
958968
private static final String PREFIX_AMQP_SECURE = "amqps://";
959969

960-
private static final int DEFAULT_PORT_SECURE = 5671;
961-
962970
private String host;
963971

964972
private int port;

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ void determineHostReturnsHostPropertyWhenNoAddresses() {
6060
}
6161

6262
@Test
63-
void portDefaultsTo5672() {
64-
assertThat(this.properties.getPort()).isEqualTo(5672);
63+
void portDefaultsToNull() {
64+
assertThat(this.properties.getPort()).isNull();
6565
}
6666

6767
@Test
@@ -76,6 +76,17 @@ void determinePortReturnsPortOfFirstAddress() {
7676
assertThat(this.properties.determinePort()).isEqualTo(1234);
7777
}
7878

79+
@Test
80+
void determinePortReturnsDefaultPortWhenNoAddresses() {
81+
assertThat(this.properties.determinePort()).isEqualTo(5672);
82+
}
83+
84+
@Test
85+
void determinePortWithSslReturnsDefaultSslPortWhenNoAddresses() {
86+
this.properties.getSsl().setEnabled(true);
87+
assertThat(this.properties.determinePort()).isEqualTo(5671);
88+
}
89+
7990
@Test
8091
void determinePortReturnsPortPropertyWhenNoAddresses() {
8192
this.properties.setPort(1234);
@@ -235,6 +246,17 @@ void determineAddressesReturnsAddressesWithJustHostAndPort() {
235246
assertThat(this.properties.determineAddresses()).isEqualTo("rabbit1.example.com:1234,rabbit2.example.com:5672");
236247
}
237248

249+
@Test
250+
void determineAddressesUsesDefaultWhenNoAddressesSet() {
251+
assertThat(this.properties.determineAddresses()).isEqualTo("localhost:5672");
252+
}
253+
254+
@Test
255+
void determineAddressesWithSslUsesDefaultWhenNoAddressesSet() {
256+
this.properties.getSsl().setEnabled(true);
257+
assertThat(this.properties.determineAddresses()).isEqualTo("localhost:5671");
258+
}
259+
238260
@Test
239261
void determineAddressesUsesHostAndPortPropertiesWhenNoAddressesSet() {
240262
this.properties.setHost("rabbit.example.com");

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5330,6 +5330,9 @@ Alternatively, you could configure the same connection using the `addresses` att
53305330
spring.rabbitmq.addresses=amqp://admin:secret@localhost
53315331
----
53325332

5333+
NOTE: When specifying addresses that way, the `host` and `port` properties are ignored.
5334+
If the address uses the `amqps` protocol, SSL support is enabled automatically.
5335+
53335336
If a `ConnectionNameStrategy` bean exists in the context, it will be automatically used to name connections created by the auto-configured `ConnectionFactory`.
53345337
See {spring-boot-autoconfigure-module-code}/amqp/RabbitProperties.java[`RabbitProperties`] for more of the supported options.
53355338

0 commit comments

Comments
 (0)