--- /dev/null
+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);
+ }
+}
+++ /dev/null
-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);
- }
-}