Version des `spring-consumer`, die einen Anwendungsfehler fängt und ignoriert consumer/spring-consumer--logic-error consumer/spring-consumer--logic-error--2024-11-13--si
authorKai Moritz <kai@juplo.de>
Sun, 10 Nov 2024 14:49:10 +0000 (15:49 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 10 Nov 2024 14:49:10 +0000 (15:49 +0100)
src/main/java/de/juplo/kafka/ExampleConsumer.java

index f832b45..9a1984f 100644 (file)
@@ -57,12 +57,24 @@ public class ExampleConsumer implements Runnable
         log.info("{} - Received {} messages", id, records.count());
         for (ConsumerRecord<String, String> record : records)
         {
-          handleRecord(
-            record.topic(),
-            record.partition(),
-            record.offset(),
-            record.key(),
-            record.value());
+          try
+          {
+            handleRecord(
+              record.topic(),
+              record.partition(),
+              record.offset(),
+              record.key(),
+              record.value());
+          }
+          catch (NumberFormatException e)
+          {
+            log.error(
+              "{} - Ignoring invalid message for offset {} on partition {}: {}",
+              id,
+              record.offset(),
+              record.partition(),
+              record.value());
+          }
         }
       }
     }
@@ -93,7 +105,8 @@ public class ExampleConsumer implements Runnable
     String value)
   {
     consumed++;
-    log.info("{} - {}: {}/{} - {}={}", id, offset, topic, partition, key, value);
+    long message = Long.parseLong(value);
+    log.info("{} - {}: {}/{} - {}={}", id, offset, topic, partition, key, message);
   }