Add kafka4-migration module: Spring Kafka 3.3 to 4.1 migration on Boot 3.5 to 4.1

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Gth99spB1CzQmBFofXmU4Z
This commit is contained in:
2026-09-28 09:19:55 +00:00
co-authored by Claude Sonnet 5
parent d56c60824e
commit c1b40b0db9
23 changed files with 308 additions and 0 deletions
@@ -0,0 +1,9 @@
package com.ankurm.kafka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) { SpringApplication.run(App.class, args); }
}
@@ -0,0 +1,3 @@
package com.ankurm.kafka;
public record OrderEvent(String id, int qty) { }
@@ -0,0 +1,18 @@
package com.ankurm.kafka;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.List;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;
@Component
public class OrderListener {
public final List<String> received = new CopyOnWriteArrayList<>();
@KafkaListener(topics = "orders")
void on(OrderEvent event, @Header(KafkaHeaders.RECEIVED_KEY) String key) {
received.add("key=" + key + " id=" + event.id() + " qty=" + event.qty());
}
}
@@ -0,0 +1,14 @@
package com.ankurm.kafka;
import java.nio.charset.StandardCharsets;
import org.springframework.kafka.support.serializer.JacksonJsonSerializer;
/** Serialises one event with the JSON serializer, using only the runtime classpath (no test jars). */
public class SerdeProbe {
public static void main(String[] args) {
try (JacksonJsonSerializer<OrderEvent> s = new JacksonJsonSerializer<>()) {
byte[] bytes = s.serialize("orders", new OrderEvent("o-1", 3));
System.out.println("SERIALIZED " + new String(bytes, StandardCharsets.UTF_8));
}
}
}
@@ -0,0 +1,8 @@
spring.kafka.consumer.group-id=orders-group
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JacksonJsonSerializer
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JacksonJsonDeserializer
spring.kafka.consumer.properties.spring.json.trusted.packages=com.ankurm.kafka
spring.kafka.consumer.properties.spring.json.value.default.type=com.ankurm.kafka.OrderEvent
@@ -0,0 +1,27 @@
package com.ankurm.kafka;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.test.EmbeddedKafkaBroker;
import org.springframework.kafka.test.context.EmbeddedKafka;
@SpringBootTest(properties = "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}")
@EmbeddedKafka(topics = "orders", partitions = 1)
class OrderFlowTest {
@Autowired KafkaTemplate<String, OrderEvent> template;
@Autowired OrderListener listener;
@Autowired EmbeddedKafkaBroker broker;
@Test
void sendAndReceive() {
template.send("orders", "k1", new OrderEvent("o-1", 3));
await().untilAsserted(() -> assertThat(listener.received).hasSize(1));
System.out.println("RESULT " + listener.received.get(0));
System.out.println("BROKERS " + broker.getBrokersAsString().replaceAll(":\\d+", ":PORT"));
}
}