Skip to content

Commit 9b09225

Browse files
GH-374: Support bootstrap server property as a list
Resolves #374 * support read bootstrap server config when it is defined as list * update copyright year of the affected classes to 2024
1 parent 57b7085 commit 9b09225

File tree

4 files changed

+126
-5
lines changed

4 files changed

+126
-5
lines changed

src/main/java/reactor/kafka/receiver/ReceiverOptions.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016-2023 VMware Inc. or its affiliates, All Rights Reserved.
2+
* Copyright (c) 2016-2024 VMware Inc. or its affiliates, All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -570,7 +570,15 @@ default String clientId() {
570570
*/
571571
@NonNull
572572
default String bootstrapServers() {
573-
return (String) Objects.requireNonNull(consumerProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG));
573+
Object bootstrapServers = Objects.requireNonNull(consumerProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG));
574+
575+
if (bootstrapServers instanceof List) {
576+
@SuppressWarnings("unchecked")
577+
List<String> listOfBootstrapServers = (List<String>) bootstrapServers;
578+
return String.join(",", listOfBootstrapServers);
579+
}
580+
581+
return (String) bootstrapServers;
574582
}
575583

576584
/**

src/main/java/reactor/kafka/sender/SenderOptions.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016-2023 VMware Inc. or its affiliates, All Rights Reserved.
2+
* Copyright (c) 2016-2024 VMware Inc. or its affiliates, All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.time.Duration;
2020
import java.util.Map;
2121
import java.util.Objects;
22+
import java.util.List;
2223
import java.util.Properties;
2324

2425
import javax.naming.AuthenticationException;
@@ -286,7 +287,15 @@ default String transactionalId() {
286287
*/
287288
@NonNull
288289
default String bootstrapServers() {
289-
return (String) Objects.requireNonNull(producerProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG));
290+
Object bootstrapServers = Objects.requireNonNull(producerProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG));
291+
292+
if (bootstrapServers instanceof List) {
293+
@SuppressWarnings("unchecked")
294+
List<String> listOfBootstrapServers = (List<String>) bootstrapServers;
295+
return String.join(",", listOfBootstrapServers);
296+
}
297+
298+
return (String) bootstrapServers;
290299
}
291300

292301
@NonNull
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2016-2024 VMware Inc. or its affiliates, All Rights Reserved.
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 reactor.kafka.receiver;
18+
19+
import org.apache.kafka.clients.consumer.ConsumerConfig;
20+
import org.junit.Test;
21+
22+
import java.util.Arrays;
23+
import java.util.Collections;
24+
import java.util.HashMap;
25+
import java.util.List;
26+
import java.util.Map;
27+
28+
import static org.junit.Assert.assertEquals;
29+
30+
public class ReceiverOptionsTest {
31+
32+
@Test
33+
public void getBootstrapServersFromSingleServerList() {
34+
Map<String, Object> producerProperties = new HashMap<>();
35+
producerProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, Collections.singletonList("localhost:9092"));
36+
37+
ReceiverOptions<Integer, String> senderOptions = ReceiverOptions.create(producerProperties);
38+
String bootstrapServers = senderOptions.bootstrapServers();
39+
40+
assertEquals("localhost:9092", bootstrapServers);
41+
}
42+
43+
@Test
44+
public void getBootstrapServersFromMultipleServersList() {
45+
Map<String, Object> producerProperties = new HashMap<>();
46+
List<String> serverList = Arrays.asList("localhost:9092", "localhost:9093", "localhost:9094");
47+
producerProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, serverList);
48+
49+
ReceiverOptions<Integer, String> senderOptions = ReceiverOptions.create(producerProperties);
50+
String bootstrapServers = senderOptions.bootstrapServers();
51+
52+
assertEquals("localhost:9092,localhost:9093,localhost:9094", bootstrapServers);
53+
}
54+
55+
@Test
56+
public void getBootstrapServersFromString() {
57+
Map<String, Object> producerProperties = new HashMap<>();
58+
producerProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
59+
60+
ReceiverOptions<Integer, String> senderOptions = ReceiverOptions.create(producerProperties);
61+
String bootstrapServers = senderOptions.bootstrapServers();
62+
63+
assertEquals("localhost:9092", bootstrapServers);
64+
}
65+
66+
}

src/test/java/reactor/kafka/sender/SenderOptionsTest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020-2021 VMware Inc. or its affiliates, All Rights Reserved.
2+
* Copyright (c) 2020-2024 VMware Inc. or its affiliates, All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,10 +16,14 @@
1616

1717
package reactor.kafka.sender;
1818

19+
import org.apache.kafka.clients.producer.ProducerConfig;
1920
import org.junit.Test;
2021

2122
import java.time.Duration;
23+
import java.util.Arrays;
2224
import java.util.Collections;
25+
import java.util.HashMap;
26+
import java.util.List;
2327
import java.util.Map;
2428

2529
import static org.junit.Assert.assertEquals;
@@ -33,4 +37,38 @@ public void senderOptionsCloseTimeout() {
3337
assertEquals(Duration.ofMillis(100), senderOptions.closeTimeout(Duration.ofMillis(100)).closeTimeout());
3438
}
3539

40+
@Test
41+
public void getBootstrapServersFromSingleServerList() {
42+
Map<String, Object> producerProperties = new HashMap<>();
43+
producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, Collections.singletonList("localhost:9092"));
44+
45+
SenderOptions<Integer, String> senderOptions = SenderOptions.create(producerProperties);
46+
String bootstrapServers = senderOptions.bootstrapServers();
47+
48+
assertEquals("localhost:9092", bootstrapServers);
49+
}
50+
51+
@Test
52+
public void getBootstrapServersFromMultipleServersList() {
53+
Map<String, Object> producerProperties = new HashMap<>();
54+
List<String> serverList = Arrays.asList("localhost:9092", "localhost:9093", "localhost:9094");
55+
producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, serverList);
56+
57+
SenderOptions<Integer, String> senderOptions = SenderOptions.create(producerProperties);
58+
String bootstrapServers = senderOptions.bootstrapServers();
59+
60+
assertEquals("localhost:9092,localhost:9093,localhost:9094", bootstrapServers);
61+
}
62+
63+
@Test
64+
public void getBootstrapServersFromString() {
65+
Map<String, Object> producerProperties = new HashMap<>();
66+
producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
67+
68+
SenderOptions<Integer, String> senderOptions = SenderOptions.create(producerProperties);
69+
String bootstrapServers = senderOptions.bootstrapServers();
70+
71+
assertEquals("localhost:9092", bootstrapServers);
72+
}
73+
3674
}

0 commit comments

Comments
 (0)