Version des ``spring-consumer``, der Deserialisierungs-Fehler überspringt consumer/spring-consumer--deserialization-error consumer/spring-consumer--deserialization-error--2024-11-13--si
authorKai Moritz <kai@juplo.de>
Sun, 10 Nov 2024 16:57:23 +0000 (17:57 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 10 Nov 2024 16:57:23 +0000 (17:57 +0100)
README.sh
docker/docker-compose.yml
pom.xml
src/main/java/de/juplo/kafka/ExampleConsumer.java

index f4696e0..ca8553a 100755 (executable)
--- a/README.sh
+++ b/README.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 
-IMAGE=juplo/spring-consumer:1.1-long-SNAPSHOT
+IMAGE=juplo/spring-consumer:1.1-deserialization-error-SNAPSHOT
 
 if [ "$1" = "cleanup" ]
 then
index b899570..6d6b7b7 100644 (file)
@@ -197,14 +197,14 @@ services:
       juplo.producer.topic: test
 
   consumer-1:
-    image: juplo/spring-consumer:1.1-long-SNAPSHOT
+    image: juplo/spring-consumer:1.1-deserialization-error-SNAPSHOT
     environment:
       juplo.bootstrap-server: kafka:9092
       juplo.client-id: consumer-1
       juplo.consumer.topic: test
 
   consumer-2:
-    image: juplo/spring-consumer:1.1-long-SNAPSHOT
+    image: juplo/spring-consumer:1.1-deserialization-error-SNAPSHOT
     environment:
       juplo.bootstrap-server: kafka:9092
       juplo.client-id: consumer-2
diff --git a/pom.xml b/pom.xml
index c32791f..abcf5cb 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -15,7 +15,7 @@
   <artifactId>spring-consumer</artifactId>
   <name>Spring Consumer</name>
   <description>Super Simple Consumer-Group, that is implemented as Spring-Boot application and configured by Spring Kafka</description>
-  <version>1.1-long-SNAPSHOT</version>
+  <version>1.1-deserialization-error-SNAPSHOT</version>
 
   <properties>
     <java.version>21</java.version>
index faa4c0a..bb0b619 100644 (file)
@@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.kafka.clients.consumer.Consumer;
 import org.apache.kafka.clients.consumer.ConsumerRecord;
 import org.apache.kafka.clients.consumer.ConsumerRecords;
+import org.apache.kafka.common.errors.RecordDeserializationException;
 import org.apache.kafka.common.errors.WakeupException;
 
 import java.time.Duration;
@@ -51,18 +52,31 @@ public class ExampleConsumer implements Runnable
 
       while (running)
       {
-        ConsumerRecords<String, Long> records =
+        try
+        {
+          ConsumerRecords<String, Long> records =
             consumer.poll(Duration.ofSeconds(1));
 
-        log.info("{} - Received {} messages", id, records.count());
-        for (ConsumerRecord<String, Long> record : records)
+          log.info("{} - Received {} messages", id, records.count());
+          for (ConsumerRecord<String, Long> record : records)
+          {
+            handleRecord(
+              record.topic(),
+              record.partition(),
+              record.offset(),
+              record.key(),
+              record.value());
+          }
+        }
+        catch (RecordDeserializationException e)
         {
-          handleRecord(
-            record.topic(),
-            record.partition(),
-            record.offset(),
-            record.key(),
-            record.value());
+          log.error(
+            "{} - Ignoring invalid record for offset {} on partition {}: {}",
+            id,
+            e.offset(),
+            e.topicPartition(),
+            e.getMessage());
+          consumer.seek(e.topicPartition(), e.offset() + 1);
         }
       }
     }