Einheitliche Benennung des Consumers -- MOVE
authorKai Moritz <kai@juplo.de>
Sun, 29 Sep 2024 07:54:56 +0000 (09:54 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 29 Sep 2024 19:22:50 +0000 (21:22 +0200)
src/main/java/de/juplo/kafka/ExampleConsumer.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/SimpleConsumer.java [deleted file]

diff --git a/src/main/java/de/juplo/kafka/ExampleConsumer.java b/src/main/java/de/juplo/kafka/ExampleConsumer.java
new file mode 100644 (file)
index 0000000..a76132b
--- /dev/null
@@ -0,0 +1,80 @@
+package de.juplo.kafka;
+
+import lombok.RequiredArgsConstructor;
+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.WakeupException;
+
+import java.time.Duration;
+import java.util.Arrays;
+import java.util.concurrent.Callable;
+
+
+@Slf4j
+@RequiredArgsConstructor
+public class SimpleConsumer implements Callable<Integer>
+{
+  private final String id;
+  private final String[] topics;
+  private final Consumer<String, String> consumer;
+
+  private long consumed = 0;
+
+
+  @Override
+  public Integer call()
+  {
+    try
+    {
+      log.info("{} - Subscribing to topics: {}", id, topics);
+      consumer.subscribe(Arrays.asList(topics));
+
+      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());
+        }
+      }
+    }
+    catch(WakeupException e)
+    {
+      log.info("{} - Consumer was signaled to finish its work", id);
+      return 0;
+    }
+    catch(Exception e)
+    {
+      log.error("{} - Unexpected error: {}, unsubscribing!", id, e.toString());
+      consumer.unsubscribe();
+      return 1;
+    }
+    finally
+    {
+      log.info("{} - Closing the KafkaConsumer", id);
+      consumer.close();
+      log.info("{}: Consumed {} messages in total, exiting!", id, consumed);
+    }
+  }
+
+  private void handleRecord(
+    String topic,
+    Integer partition,
+    Long offset,
+    String key,
+    String value)
+  {
+    consumed++;
+    log.info("{} - {}: {}/{} - {}={}", id, offset, topic, partition, key, value);
+  }
+}
diff --git a/src/main/java/de/juplo/kafka/SimpleConsumer.java b/src/main/java/de/juplo/kafka/SimpleConsumer.java
deleted file mode 100644 (file)
index a76132b..0000000
+++ /dev/null
@@ -1,80 +0,0 @@
-package de.juplo.kafka;
-
-import lombok.RequiredArgsConstructor;
-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.WakeupException;
-
-import java.time.Duration;
-import java.util.Arrays;
-import java.util.concurrent.Callable;
-
-
-@Slf4j
-@RequiredArgsConstructor
-public class SimpleConsumer implements Callable<Integer>
-{
-  private final String id;
-  private final String[] topics;
-  private final Consumer<String, String> consumer;
-
-  private long consumed = 0;
-
-
-  @Override
-  public Integer call()
-  {
-    try
-    {
-      log.info("{} - Subscribing to topics: {}", id, topics);
-      consumer.subscribe(Arrays.asList(topics));
-
-      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());
-        }
-      }
-    }
-    catch(WakeupException e)
-    {
-      log.info("{} - Consumer was signaled to finish its work", id);
-      return 0;
-    }
-    catch(Exception e)
-    {
-      log.error("{} - Unexpected error: {}, unsubscribing!", id, e.toString());
-      consumer.unsubscribe();
-      return 1;
-    }
-    finally
-    {
-      log.info("{} - Closing the KafkaConsumer", id);
-      consumer.close();
-      log.info("{}: Consumed {} messages in total, exiting!", id, consumed);
-    }
-  }
-
-  private void handleRecord(
-    String topic,
-    Integer partition,
-    Long offset,
-    String key,
-    String value)
-  {
-    consumed++;
-    log.info("{} - {}: {}/{} - {}={}", id, offset, topic, partition, key, value);
-  }
-}