import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;
-import java.util.concurrent.locks.Condition;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
@Slf4j
public class SimpleConsumer
{
- private long consumed = 0;
- private KafkaConsumer<String, String> consumer;
- private Lock lock = new ReentrantLock();
- private Condition stopped = lock.newCondition();
+ private final String id;
+ private final String topic;
+ private final KafkaConsumer<String, String> consumer;
+ private volatile boolean running = false;
+ private long consumed = 0;
- public SimpleConsumer()
+ public SimpleConsumer(String broker, String topic, String groupId, String clientId)
{
- // tag::create[]
Properties props = new Properties();
- props.put("bootstrap.servers", ":9092");
- props.put("group.id", "my-consumer"); // << Used for Offset-Commits
- // end::create[]
- props.put("auto.offset.reset", "earliest");
- // tag::create[]
+ 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());
- KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
- // end::create[]
- this.consumer = consumer;
+ consumer = new KafkaConsumer<>(props);
+
+ this.topic = topic;
+ this.id = clientId;
}
public void run()
{
- String id = "C";
-
try
{
log.info("{} - Subscribing to topic test", id);
- consumer.subscribe(Arrays.asList("test"));
+ // TODO: subscribe!
+ running = true;
- // tag::loop[]
while (true)
{
- ConsumerRecords<String, String> records =
- consumer.poll(Duration.ofSeconds(1));
-
- // Do something with the data...
- // end::loop[]
- log.info("{} - Received {} messages", id, records.count());
- for (ConsumerRecord<String, String> record : records)
- {
- consumed++;
- log.info(
- "{} - {}: {}/{} - {}={}",
- id,
- record.offset(),
- record.topic(),
- record.partition(),
- record.key(),
- record.value()
- );
- }
- // tag::loop[]
+ // TODO:
+ // * Poll for new records
+ // * Loop through received records
+ // * Count received records in the variable `consumed`
+ // * "Work" with the data: Log partition/offset/key/value
}
- // end::loop[]
}
catch(WakeupException e)
{
- log.info("{} - RIIING!", id);
+ log.info("{} - Consumer was signaled to finish its work", id);
}
catch(Exception e)
{
}
finally
{
- this.lock.lock();
- try
- {
- log.info("{} - Closing the KafkaConsumer", id);
- consumer.close();
- log.info("C - DONE!");
- stopped.signal();
- }
- finally
- {
- this.lock.unlock();
- log.info("{}: Consumed {} messages in total, exiting!", id, consumed);
- }
+ running = false;
+ log.info("{} - Closing the KafkaConsumer", id);
+ consumer.close();
+ log.info("{}: Consumed {} messages in total, exiting!", id, consumed);
}
}
public static void main(String[] args) throws Exception
{
- SimpleConsumer instance = new SimpleConsumer();
+ String broker = ":9092";
+ String topic = "test";
+ String groupId = "my-group";
+ String clientId = "DEV";
+
+ switch (args.length)
+ {
+ case 4:
+ clientId = args[3];
+ case 3:
+ groupId = args[2];
+ case 2:
+ topic = args[1];
+ case 1:
+ broker = args[0];
+ }
+
+
+ SimpleConsumer instance = new SimpleConsumer(broker, topic, groupId, clientId);
Runtime.getRuntime().addShutdownHook(new Thread(() ->
{
- instance.lock.lock();
- try
- {
- instance.consumer.wakeup();
- instance.stopped.await();
- }
- catch (InterruptedException e)
- {
- log.warn("Interrrupted while waiting for the consumer to stop!", e);
- }
- finally
+ instance.consumer.wakeup();
+
+ while (instance.running)
{
- instance.lock.unlock();
+ log.info("Waiting for main-thread...");
+ try
+ {
+ Thread.sleep(1000);
+ }
+ catch (InterruptedException e) {}
}
+ log.info("Shutdown completed.");
}));
+ log.info(
+ "Running SimpleConsumer: broker={}, topic={}, group-id={}, client-id={}",
+ broker,
+ topic,
+ groupId,
+ clientId);
instance.run();
}
}