From: Kai Moritz Date: Sun, 29 Sep 2024 07:54:56 +0000 (+0200) Subject: Einheitliche Benennung des Consumers -- MOVE X-Git-Tag: consumer/spring-consumer--BRANCH-ENDE~12 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=ca1175bb5b0412638a73ad2a446880800e289ac2;p=demos%2Fkafka%2Ftraining Einheitliche Benennung des Consumers -- MOVE --- diff --git a/src/main/java/de/juplo/kafka/ExampleConsumer.java b/src/main/java/de/juplo/kafka/ExampleConsumer.java new file mode 100644 index 0000000..a76132b --- /dev/null +++ b/src/main/java/de/juplo/kafka/ExampleConsumer.java @@ -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 +{ + private final String id; + private final String[] topics; + private final Consumer 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 records = + consumer.poll(Duration.ofSeconds(1)); + + log.info("{} - Received {} messages", id, records.count()); + for (ConsumerRecord 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 index a76132b..0000000 --- a/src/main/java/de/juplo/kafka/SimpleConsumer.java +++ /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 -{ - private final String id; - private final String[] topics; - private final Consumer 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 records = - consumer.poll(Duration.ofSeconds(1)); - - log.info("{} - Received {} messages", id, records.count()); - for (ConsumerRecord 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); - } -}