1 package de.juplo.kafka;
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.consumer.ConsumerRecord;
5 import org.apache.kafka.clients.consumer.ConsumerRecords;
6 import org.apache.kafka.clients.consumer.KafkaConsumer;
7 import org.apache.kafka.common.errors.WakeupException;
8 import org.apache.kafka.common.serialization.StringDeserializer;
10 import java.time.Duration;
11 import java.util.Arrays;
12 import java.util.Properties;
16 public class SimpleConsumer
18 private final String id;
19 private final String topic;
20 private final KafkaConsumer<String, String> consumer;
22 private volatile boolean running = false;
23 private long consumed = 0;
25 public SimpleConsumer(String broker, String topic, String groupId, String clientId)
27 Properties props = new Properties();
28 props.put("bootstrap.servers", broker);
29 props.put("group.id", groupId); // ID für die Offset-Commits
30 props.put("client.id", clientId); // Nur zur Wiedererkennung
31 props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.CooperativeStickyAssignor");
32 props.put("key.deserializer", StringDeserializer.class.getName());
33 props.put("value.deserializer", StringDeserializer.class.getName());
35 consumer = new KafkaConsumer<>(props);
46 log.info("{} - Subscribing to topic test", id);
47 consumer.subscribe(Arrays.asList("test"));
52 ConsumerRecords<String, String> records =
53 consumer.poll(Duration.ofSeconds(1));
55 log.info("{} - Received {} messages", id, records.count());
56 for (ConsumerRecord<String, String> record : records)
60 "{} - {}: {}/{} - {}={}",
71 catch(WakeupException e)
73 log.info("{} - Consumer was signaled to finish its work", id);
77 log.error("{} - Unexpected error: {}", id, e.toString());
82 log.info("{} - Closing the KafkaConsumer", id);
84 log.info("{}: Consumed {} messages in total, exiting!", id, consumed);
89 public static void main(String[] args) throws Exception
91 String broker = ":9092";
92 String topic = "test";
93 String groupId = "my-group";
94 String clientId = "DEV";
109 SimpleConsumer instance = new SimpleConsumer(broker, topic, groupId, clientId);
111 Runtime.getRuntime().addShutdownHook(new Thread(() ->
113 instance.consumer.wakeup();
115 while (instance.running)
117 log.info("Waiting for main-thread...");
122 catch (InterruptedException e) {}
124 log.info("Shutdown completed.");
128 "Running SimpleConsumer: broker={}, topic={}, group-id={}, client-id={}",