Skip to content

Commit c1972f6

Browse files
csk8167scottfrederick
csk8167
authored andcommitted
Support IPv6 addresses when configuring RabbitMQ using properties
See gh-37619
1 parent a1947d6 commit c1972f6

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ public String getVirtualHost() {
5353
public List<Address> getAddresses() {
5454
List<Address> addresses = new ArrayList<>();
5555
for (String address : this.properties.determineAddresses().split(",")) {
56-
String[] components = address.split(":");
57-
addresses.add(new Address(components[0], Integer.parseInt(components[1])));
56+
int portSeparatorPosition = address.lastIndexOf(':');
57+
String host = address.substring(0, portSeparatorPosition);
58+
String port = address.substring(portSeparatorPosition + 1);
59+
addresses.add(new Address(host, Integer.parseInt(port)));
5860
}
5961
return addresses;
6062
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.amqp;
18+
19+
import java.util.List;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.boot.autoconfigure.amqp.RabbitConnectionDetails.Address;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.junit.jupiter.api.Assertions.*;
27+
28+
class PropertiesRabbitConnectionDetailsTest {
29+
30+
@Test
31+
void getAddresses() {
32+
RabbitProperties properties = new RabbitProperties();
33+
properties.setAddresses("localhost:1234,[::1]:32863");
34+
PropertiesRabbitConnectionDetails propertiesRabbitConnectionDetails = new PropertiesRabbitConnectionDetails(properties);
35+
List<Address> addresses = propertiesRabbitConnectionDetails.getAddresses();
36+
assertThat(addresses.size()).isEqualTo(2);
37+
assertThat(addresses.get(0).host()).isEqualTo("localhost");
38+
assertThat(addresses.get(1).host()).isEqualTo("[::1]");
39+
assertThat(addresses.get(0).port()).isEqualTo(1234);
40+
assertThat(addresses.get(1).port()).isEqualTo(32863);
41+
}
42+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ void determineAddressesWithSslUsesDefaultWhenNoAddressesSet() {
274274
assertThat(this.properties.determineAddresses()).isEqualTo("localhost:5671");
275275
}
276276

277+
@Test
278+
void determineAddressesUsesHostAndPortPropertiesWhenNoAddressesSetIpV6() {
279+
this.properties.setHost("[::1]");
280+
this.properties.setPort(32863);
281+
assertThat(this.properties.determineAddresses()).isEqualTo("[::1]:32863");
282+
}
283+
277284
@Test
278285
void determineAddressesUsesHostAndPortPropertiesWhenNoAddressesSet() {
279286
this.properties.setHost("rabbit.example.com");

0 commit comments

Comments
 (0)