Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-3662: Support Kafka 3.9.0+ in EmbeddedKafkaKraftBroker #3665

Merged
merged 4 commits into from
Dec 13, 2024

Conversation

sobychacko
Copy link
Contributor

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.

Fixes: spring-projects#3662
Issue: spring-projects#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.
}
else {
// For Kafka 3.8.0: setConfigProp(String, String)
Method setConfigMethod = builderClass.getMethod("setConfigProp", String.class, String.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a reflection for this method?

Please, consider to find method via reflection only one in static block of the class.


private static boolean isClassicConsumerPresent() {
try {
Class.forName(CLASS_EXISTS_ONLY_IN_390);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See ClassUtils.isPresent().

if (isKafka39OrLater) {
// For Kafka 3.9.0+: setConfigProp(String, Object)
Method setConfigMethod = builderClass.getMethod("setConfigProp", String.class, Object.class);
setConfigMethod.invoke(clusterBuilder, (String) key, value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also see ReflectionUtils.findMethod() and ReflectionUtils.invokeMethod().
You won't need to deal with any exceptions then!

private static final Method SET_CONFIG_METHOD;

static {
IS_KAFKA_39_OR_LATER = ClassUtils.isPresent(CLASS_EXISTS_ONLY_IN_390, EmbeddedKafkaKraftBroker.class.getClassLoader());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isPresent() doesn't throw an exception: can be used directly in the property declaration line.
Also: I don't see a reason in the CLASS_EXISTS_ONLY_IN_390 constant. You simply can use that class name in this method call.

@@ -243,6 +266,17 @@ private void start() {
System.setProperty(SPRING_EMBEDDED_KAFKA_BROKERS, getBrokersAsString());
}

private static void setConfigProperty(KafkaClusterTestKit.Builder clusterBuilder, Object key, Object value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit-pick: can we cast key into a String where we call this method?
That way that cast operation would happen only in one place.

@artembilan artembilan merged commit 831dba1 into spring-projects:main Dec 13, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow kafka-clients 3.9.x with Spring-Kafka 3.3.x
2 participants