Spring-Kafka Version des Simple-Consumer auf JSON-Nachrichten umgestellt
[demos/kafka/training] / src / main / java / de / juplo / kafka / SimpleConsumer.java
index 0e37686..3b0202f 100644 (file)
@@ -17,7 +17,7 @@ public class SimpleConsumer implements Runnable
 {
   private final String id;
   private final String topic;
-  private final Consumer<String, String> consumer;
+  private final Consumer<String, Message> consumer;
 
   private long consumed = 0;
 
@@ -32,22 +32,18 @@ public class SimpleConsumer implements Runnable
 
       while (true)
       {
-        ConsumerRecords<String, String> records =
+        ConsumerRecords<String, Message> records =
             consumer.poll(Duration.ofSeconds(1));
 
         log.info("{} - Received {} messages", id, records.count());
-        for (ConsumerRecord<String, String> record : records)
+        for (ConsumerRecord<String, Message> record : records)
         {
-          consumed++;
-          log.info(
-              "{} - {}: {}/{} - {}={}",
-              id,
-              record.offset(),
-              record.topic(),
-              record.partition(),
-              record.key(),
-              record.value()
-          );
+          handleRecord(
+            record.topic(),
+            record.partition(),
+            record.offset(),
+            record.key(),
+            record.value());
         }
       }
     }
@@ -67,4 +63,15 @@ public class SimpleConsumer implements Runnable
       log.info("{}: Consumed {} messages in total, exiting!", id, consumed);
     }
   }
+
+  private void handleRecord(
+    String topic,
+    Integer partition,
+    Long offset,
+    String key,
+    Message value)
+  {
+    consumed++;
+    log.info("{} - {}: {}/{} - {}={}", id, offset, topic, partition, key, value);
+  }
 }