From: Kai Moritz Date: Tue, 22 Nov 2022 18:29:32 +0000 (+0100) Subject: Vorlage X-Git-Tag: grundlagen/simple-consumer--vorlage--2024-11-13--si X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Fgrundlagen%2Fsimple-consumer--vorlage;p=demos%2Fkafka%2Ftraining Vorlage --- diff --git a/src/main/java/de/juplo/kafka/ExampleConsumer.java b/src/main/java/de/juplo/kafka/ExampleConsumer.java index 7fbd5de..3634875 100644 --- a/src/main/java/de/juplo/kafka/ExampleConsumer.java +++ b/src/main/java/de/juplo/kafka/ExampleConsumer.java @@ -30,14 +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("auto.offset.reset", "earliest"); // Von Beginn an lesen - props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.CooperativeStickyAssignor"); - props.put("key.deserializer", StringDeserializer.class.getName()); - props.put("value.deserializer", StringDeserializer.class.getName()); - props.put("metadata.maxage.ms", 5000); + // Konfiguration für den Consumer zusammenstellen this.id = clientId; this.topic = topic; @@ -50,24 +43,15 @@ public class ExampleConsumer try { log.info("{} - Subscribing to topic {}", id, topic); - consumer.subscribe(Arrays.asList(topic)); + // TODO: subscribe! running = true; 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()); - } + // TODO: + // Über consumer.poll() Nachrichten abrufen und für die + // empfangenen Nachrichten die Methode handleRecord + // aufrufen. } } catch(WakeupException e) @@ -77,7 +61,6 @@ public class ExampleConsumer catch(Exception e) { log.error("{} - Unexpected error, unsubscribing!", id, e); - consumer.unsubscribe(); } finally {