Vorlage grundlagen/simple-consumer--vorlage--COMMITS--2025-02
authorKai Moritz <kai@juplo.de>
Tue, 22 Nov 2022 18:29:32 +0000 (19:29 +0100)
committerKai Moritz <kai@juplo.de>
Thu, 6 Feb 2025 16:47:51 +0000 (17:47 +0100)
src/main/java/de/juplo/kafka/ExampleConsumer.java

index f9da825..9213b1c 100644 (file)
@@ -30,11 +30,7 @@ public class ExampleConsumer
     String clientId)
   {
     Properties props = new Properties();
-    props.put("bootstrap.servers", broker);
-    props.put("group.id", groupId); // ID für die Offset-Commits
-    props.put("client.id", clientId); // Nur zur Wiedererkennung
-    props.put("key.deserializer", StringDeserializer.class.getName());
-    props.put("value.deserializer", StringDeserializer.class.getName());
+    // Konfiguration für den Consumer zusammenstellen
 
     this.id = clientId;
     this.topic = topic;
@@ -47,24 +43,13 @@ public class ExampleConsumer
     try
     {
       log.info("{} - Subscribing to topic {}", id, topic);
-      consumer.subscribe(Arrays.asList(topic));
+      // TODO: subscribe!
       running = true;
 
       while (true)
       {
-        ConsumerRecords<String, String> records =
-            consumer.poll(Duration.ofSeconds(1));
-
-        log.info("{} - Received {} messages", id, records.count());
-        for (ConsumerRecord<String, String> record : records)
-        {
-          handleRecord(
-            record.topic(),
-            record.partition(),
-            record.offset(),
-            record.key(),
-            record.value());
-        }
+        // TODO:
+        // Über consumer.poll() Nachrichten abrufen und loggen
       }
     }
     catch(WakeupException e)
@@ -74,7 +59,6 @@ public class ExampleConsumer
     catch(Exception e)
     {
       log.error("{} - Unexpected error, unsubscribing!", id, e);
-      consumer.unsubscribe();
     }
     finally
     {
@@ -85,17 +69,6 @@ public class ExampleConsumer
     }
   }
 
-  private void handleRecord(
-    String topic,
-    Integer partition,
-    Long offset,
-    String key,
-    String value)
-  {
-    consumed++;
-    log.info("{} - partition={}-{}, offset={}: {}={}", id, topic, partition, offset, key, value);
-  }
-
 
   public static void main(String[] args) throws Exception
   {