]> juplo.de Git - demos/kafka/training/commitdiff
Version des ``spring-consumer``, der Deserialisierungs-Fehler überspringt consumer/spring-consumer--deserialization-error--2026-03-21--smartlifecycle-only
authorKai Moritz <kai@juplo.de>
Sun, 10 Nov 2024 16:57:23 +0000 (17:57 +0100)
committerKai Moritz <kai@juplo.de>
Fri, 20 Mar 2026 18:53:53 +0000 (19:53 +0100)
README.sh
build.gradle
docker/docker-compose.yml
pom.xml
src/main/java/de/juplo/kafka/ExampleConsumer.java

index 86b9806150304acd30bf8b46dc8f8d1267aa433a..392b237ece8ed0ad15f455c4c762d612c0e75138 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 e5c72b5a3a5626605359765ca283510fa8d38893..5d75f1187679e5526ac00c65caaa6a07e1fab14b 100644 (file)
@@ -8,7 +8,7 @@ plugins {
 }
 
 group = 'de.juplo.kafka'
-version = '1.1-long-SNAPSHOT'
+version = '1.1-deserialization-error-SNAPSHOT'
 
 java {
        toolchain {
index 81b08688622cffa33c923b03e0f894b31ceef9d2..c5dd1513be5a6752bea046f3a3722cad6fe184e3 100644 (file)
@@ -186,13 +186,13 @@ services:
       juplo.client-id: consumer
 
   peter:
-    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: peter
 
   ute:
-    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: ute
diff --git a/pom.xml b/pom.xml
index 599837b5a691e1715e0173cf62bff2c7b0a695a2..fd861de8d679625cdf7f8045b3be45e7ced7fd76 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 30711120d51777a92d0e84f6a1e52000f6d849ce..b961dfdd1f3cc24838ea4e7a207d6fa9a00fa115 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 org.springframework.context.SmartLifecycle;
 
@@ -57,17 +58,30 @@ public class ExampleConsumer implements Runnable, SmartLifecycle
 
       while (true)
       {
-        ConsumerRecords<String, Long> records = consumer.poll(Duration.ofSeconds(1));
-
-        log.info("{} - Received {} messages", id, records.count());
-        for (ConsumerRecord<String, Long> record : records)
+        try
+        {
+          ConsumerRecords<String, Long> records = consumer.poll(Duration.ofSeconds(1));
+
+          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);
         }
       }
     }