Skip to content

Commit 831dba1

Browse files
authored
GH-3662: Support Kafka 3.9.0+ in EmbeddedKafkaKraftBroker
Fixes: #3662 Issue: #3662 Add compatibility for both Kafka 3.8.0 and 3.9.0+ by handling different method signatures for setConfigProp: - 3.9.0+: setConfigProp(String, Object) - 3.8.0: setConfigProp(String, String) The change uses reflection to detect Kafka version and call appropriate method. * Direct call to setConfigProp when using 3.8.0 * Addressing PR review * Addressing PR review
1 parent 0db6b81 commit 831dba1

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

spring-kafka-test/src/main/java/org/springframework/kafka/test/EmbeddedKafkaKraftBroker.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.io.UncheckedIOException;
21+
import java.lang.reflect.Method;
2122
import java.nio.file.Files;
2223
import java.time.Duration;
2324
import java.util.AbstractMap.SimpleEntry;
@@ -56,6 +57,8 @@
5657

5758
import org.springframework.core.log.LogAccessor;
5859
import org.springframework.util.Assert;
60+
import org.springframework.util.ClassUtils;
61+
import org.springframework.util.ReflectionUtils;
5962

6063
/**
6164
* An embedded Kafka Broker(s) using KRaft.
@@ -87,6 +90,23 @@ public class EmbeddedKafkaKraftBroker implements EmbeddedKafkaBroker {
8790

8891
public static final int DEFAULT_ADMIN_TIMEOUT = 10;
8992

93+
private static final boolean IS_KAFKA_39_OR_LATER = ClassUtils.isPresent(
94+
"org.apache.kafka.server.config.AbstractKafkaConfig", EmbeddedKafkaKraftBroker.class.getClassLoader());
95+
96+
private static final Method SET_CONFIG_METHOD;
97+
98+
static {
99+
if (IS_KAFKA_39_OR_LATER) {
100+
SET_CONFIG_METHOD = ReflectionUtils.findMethod(
101+
KafkaClusterTestKit.Builder.class,
102+
"setConfigProp",
103+
String.class, Object.class);
104+
}
105+
else {
106+
SET_CONFIG_METHOD = null;
107+
}
108+
}
109+
90110
private final int count;
91111

92112
private final Set<String> topics;
@@ -212,7 +232,7 @@ private void start() {
212232
.setNumBrokerNodes(this.count)
213233
.setNumControllerNodes(this.count)
214234
.build());
215-
this.brokerProperties.forEach((k, v) -> clusterBuilder.setConfigProp((String) k, (String) v));
235+
this.brokerProperties.forEach((k, v) -> setConfigProperty(clusterBuilder, (String) k, v));
216236
this.cluster = clusterBuilder.build();
217237
}
218238
catch (Exception ex) {
@@ -238,6 +258,17 @@ private void start() {
238258
System.setProperty(SPRING_EMBEDDED_KAFKA_BROKERS, getBrokersAsString());
239259
}
240260

261+
private static void setConfigProperty(KafkaClusterTestKit.Builder clusterBuilder, String key, Object value) {
262+
if (IS_KAFKA_39_OR_LATER) {
263+
// For Kafka 3.9.0+: use reflection
264+
ReflectionUtils.invokeMethod(SET_CONFIG_METHOD, clusterBuilder, key, value);
265+
}
266+
else {
267+
// For Kafka 3.8.0: direct call
268+
clusterBuilder.setConfigProp(key, (String) value);
269+
}
270+
}
271+
241272
@Override
242273
public void destroy() {
243274
AtomicReference<Throwable> shutdownFailure = new AtomicReference<>();

0 commit comments

Comments
 (0)