From: Kai Moritz Date: Sun, 10 Nov 2024 14:49:10 +0000 (+0100) Subject: Version des `spring-consumer`, die einen Anwendungsfehler fängt und ignoriert X-Git-Tag: consumer/spring-consumer--logic-error--2024-11-13--si X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Fconsumer%2Fspring-consumer--logic-error;p=demos%2Fkafka%2Ftraining Version des `spring-consumer`, die einen Anwendungsfehler fängt und ignoriert --- diff --git a/src/main/java/de/juplo/kafka/ExampleConsumer.java b/src/main/java/de/juplo/kafka/ExampleConsumer.java index f832b45..9a1984f 100644 --- a/src/main/java/de/juplo/kafka/ExampleConsumer.java +++ b/src/main/java/de/juplo/kafka/ExampleConsumer.java @@ -57,12 +57,24 @@ public class ExampleConsumer implements Runnable log.info("{} - Received {} messages", id, records.count()); for (ConsumerRecord 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); }