Commit 722d3746 authored by Stephane Nicoll's avatar Stephane Nicoll

Rationalize RabbitProperties

Closes gh-18830
parent ef592eae
...@@ -20,6 +20,7 @@ import java.time.Duration; ...@@ -20,6 +20,7 @@ import java.time.Duration;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
import org.springframework.amqp.core.AcknowledgeMode; import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.CacheMode; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.CacheMode;
...@@ -45,15 +46,20 @@ import org.springframework.util.StringUtils; ...@@ -45,15 +46,20 @@ import org.springframework.util.StringUtils;
@ConfigurationProperties(prefix = "spring.rabbitmq") @ConfigurationProperties(prefix = "spring.rabbitmq")
public class RabbitProperties { public class RabbitProperties {
private static final int DEFAULT_PORT = 5672;
private static final int DEFAULT_PORT_SECURE = 5671;
/** /**
* RabbitMQ host. * RabbitMQ host. Ignored if an address is set.
*/ */
private String host = "localhost"; private String host = "localhost";
/** /**
* RabbitMQ port. * RabbitMQ port. Ignored if an address is set. Default to 5672, or 5671 if SSL is
* enabled.
*/ */
private int port = 5672; private Integer port;
/** /**
* Login user to authenticate to the broker. * Login user to authenticate to the broker.
...@@ -76,7 +82,8 @@ public class RabbitProperties { ...@@ -76,7 +82,8 @@ public class RabbitProperties {
private String virtualHost; private String virtualHost;
/** /**
* Comma-separated list of addresses to which the client should connect. * Comma-separated list of addresses to which the client should connect. When set, the
* host and port are ignored.
*/ */
private String addresses; private String addresses;
...@@ -143,7 +150,7 @@ public class RabbitProperties { ...@@ -143,7 +150,7 @@ public class RabbitProperties {
this.host = host; this.host = host;
} }
public int getPort() { public Integer getPort() {
return this.port; return this.port;
} }
...@@ -156,13 +163,16 @@ public class RabbitProperties { ...@@ -156,13 +163,16 @@ public class RabbitProperties {
*/ */
public int determinePort() { public int determinePort() {
if (CollectionUtils.isEmpty(this.parsedAddresses)) { if (CollectionUtils.isEmpty(this.parsedAddresses)) {
return getPort(); Integer port = getPort();
if (port != null) {
return port;
}
return (Optional.ofNullable(getSsl().getEnabled()).orElse(false)) ? DEFAULT_PORT_SECURE : DEFAULT_PORT;
} }
Address address = this.parsedAddresses.get(0); return this.parsedAddresses.get(0).port;
return address.port;
} }
public void setPort(int port) { public void setPort(Integer port) {
this.port = port; this.port = port;
} }
...@@ -177,7 +187,7 @@ public class RabbitProperties { ...@@ -177,7 +187,7 @@ public class RabbitProperties {
*/ */
public String determineAddresses() { public String determineAddresses() {
if (CollectionUtils.isEmpty(this.parsedAddresses)) { if (CollectionUtils.isEmpty(this.parsedAddresses)) {
return this.host + ":" + this.port; return this.host + ":" + determinePort();
} }
List<String> addressStrings = new ArrayList<>(); List<String> addressStrings = new ArrayList<>();
for (Address parsedAddress : this.parsedAddresses) { for (Address parsedAddress : this.parsedAddresses) {
...@@ -194,7 +204,7 @@ public class RabbitProperties { ...@@ -194,7 +204,7 @@ public class RabbitProperties {
private List<Address> parseAddresses(String addresses) { private List<Address> parseAddresses(String addresses) {
List<Address> parsedAddresses = new ArrayList<>(); List<Address> parsedAddresses = new ArrayList<>();
for (String address : StringUtils.commaDelimitedListToStringArray(addresses)) { for (String address : StringUtils.commaDelimitedListToStringArray(addresses)) {
parsedAddresses.add(new Address(address, getSsl().isEnabled())); parsedAddresses.add(new Address(address, Optional.ofNullable(getSsl().getEnabled()).orElse(false)));
} }
return parsedAddresses; return parsedAddresses;
} }
...@@ -327,9 +337,10 @@ public class RabbitProperties { ...@@ -327,9 +337,10 @@ public class RabbitProperties {
public class Ssl { public class Ssl {
/** /**
* Whether to enable SSL support. * Whether to enable SSL support. Determined automatically if an address is
* provided with the protocol (amqp:// vs. amqps://).
*/ */
private boolean enabled; private Boolean enabled;
/** /**
* Path to the key store that holds the SSL certificate. * Path to the key store that holds the SSL certificate.
...@@ -376,7 +387,7 @@ public class RabbitProperties { ...@@ -376,7 +387,7 @@ public class RabbitProperties {
*/ */
private boolean verifyHostname = true; private boolean verifyHostname = true;
public boolean isEnabled() { public Boolean getEnabled() {
return this.enabled; return this.enabled;
} }
...@@ -385,17 +396,18 @@ public class RabbitProperties { ...@@ -385,17 +396,18 @@ public class RabbitProperties {
* enabled flag if no addresses have been set. * enabled flag if no addresses have been set.
* @return whether ssl is enabled * @return whether ssl is enabled
* @see #setAddresses(String) * @see #setAddresses(String)
* @see #isEnabled() * @see #getEnabled() ()
*/ */
public boolean determineEnabled() { public boolean determineEnabled() {
boolean defaultEnabled = Optional.ofNullable(getEnabled()).orElse(false);
if (CollectionUtils.isEmpty(RabbitProperties.this.parsedAddresses)) { if (CollectionUtils.isEmpty(RabbitProperties.this.parsedAddresses)) {
return isEnabled(); return defaultEnabled;
} }
Address address = RabbitProperties.this.parsedAddresses.get(0); Address address = RabbitProperties.this.parsedAddresses.get(0);
return address.determineSslEnabled(isEnabled()); return address.determineSslEnabled(defaultEnabled);
} }
public void setEnabled(boolean enabled) { public void setEnabled(Boolean enabled) {
this.enabled = enabled; this.enabled = enabled;
} }
...@@ -953,12 +965,8 @@ public class RabbitProperties { ...@@ -953,12 +965,8 @@ public class RabbitProperties {
private static final String PREFIX_AMQP = "amqp://"; private static final String PREFIX_AMQP = "amqp://";
private static final int DEFAULT_PORT = 5672;
private static final String PREFIX_AMQP_SECURE = "amqps://"; 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;
......
...@@ -60,8 +60,8 @@ class RabbitPropertiesTests { ...@@ -60,8 +60,8 @@ class RabbitPropertiesTests {
} }
@Test @Test
void portDefaultsTo5672() { void portDefaultsToNull() {
assertThat(this.properties.getPort()).isEqualTo(5672); assertThat(this.properties.getPort()).isNull();
} }
@Test @Test
...@@ -76,6 +76,17 @@ class RabbitPropertiesTests { ...@@ -76,6 +76,17 @@ class RabbitPropertiesTests {
assertThat(this.properties.determinePort()).isEqualTo(1234); assertThat(this.properties.determinePort()).isEqualTo(1234);
} }
@Test
void determinePortReturnsDefaultPortWhenNoAddresses() {
assertThat(this.properties.determinePort()).isEqualTo(5672);
}
@Test
void determinePortWithSslReturnsDefaultSslPortWhenNoAddresses() {
this.properties.getSsl().setEnabled(true);
assertThat(this.properties.determinePort()).isEqualTo(5671);
}
@Test @Test
void determinePortReturnsPortPropertyWhenNoAddresses() { void determinePortReturnsPortPropertyWhenNoAddresses() {
this.properties.setPort(1234); this.properties.setPort(1234);
...@@ -235,6 +246,17 @@ class RabbitPropertiesTests { ...@@ -235,6 +246,17 @@ class RabbitPropertiesTests {
assertThat(this.properties.determineAddresses()).isEqualTo("rabbit1.example.com:1234,rabbit2.example.com:5672"); assertThat(this.properties.determineAddresses()).isEqualTo("rabbit1.example.com:1234,rabbit2.example.com:5672");
} }
@Test
void determineAddressesUsesDefaultWhenNoAddressesSet() {
assertThat(this.properties.determineAddresses()).isEqualTo("localhost:5672");
}
@Test
void determineAddressesWithSslUsesDefaultWhenNoAddressesSet() {
this.properties.getSsl().setEnabled(true);
assertThat(this.properties.determineAddresses()).isEqualTo("localhost:5671");
}
@Test @Test
void determineAddressesUsesHostAndPortPropertiesWhenNoAddressesSet() { void determineAddressesUsesHostAndPortPropertiesWhenNoAddressesSet() {
this.properties.setHost("rabbit.example.com"); this.properties.setHost("rabbit.example.com");
......
...@@ -5330,6 +5330,9 @@ Alternatively, you could configure the same connection using the `addresses` att ...@@ -5330,6 +5330,9 @@ Alternatively, you could configure the same connection using the `addresses` att
spring.rabbitmq.addresses=amqp://admin:secret@localhost spring.rabbitmq.addresses=amqp://admin:secret@localhost
---- ----
NOTE: When specifying addresses that way, the `host` and `port` properties are ignored.
If the address uses the `amqps` protocol, SSL support is enabled automatically.
If a `ConnectionNameStrategy` bean exists in the context, it will be automatically used to name connections created by the auto-configured `ConnectionFactory`. If a `ConnectionNameStrategy` bean exists in the context, it will be automatically used to name connections created by the auto-configured `ConnectionFactory`.
See {spring-boot-autoconfigure-module-code}/amqp/RabbitProperties.java[`RabbitProperties`] for more of the supported options. See {spring-boot-autoconfigure-module-code}/amqp/RabbitProperties.java[`RabbitProperties`] for more of the supported options.
......
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