I am currently using org.apache.kafka.clients.consumer.Consumer
, but I am open to any java client if I can ensure what I need.
Is it possible to poll one message from each partition of the requested topic using a single group-id
?
My case is that I want to poll the last current message from each partition on a particular topic. I have an implementation where I created one consumer per partition and just polled one time for each - smth like that:
final String topic = "requested-topic";
final Consumer<String, ParcelInboxEvent> consumer1 = jsonConsumerFactory.createConsumer("monitor");
final Consumer<String, ParcelInboxEvent> consumer2 = jsonConsumerFactory.createConsumer("monitor");
consumer1.assign(List.of(new TopicPartition(topic, 0)));
consumer1.seekToEnd(List.of(new TopicPartition(topic, 0)));
consumer1.seek(new TopicPartition(topic, 0), consumer1.position(new TopicPartition(topic, 0)) - 1);
consumer2.assign(List.of(new TopicPartition(topic, 1)));
consumer2.seekToEnd(List.of(new TopicPartition(topic, 1)));
consumer2.seek(new TopicPartition(topic, 1), consumer2.position(new TopicPartition(topic, 1)) - 1);
final ConsumerRecords<String, Object> poll1 = consumer1.poll(Duration.ofSeconds(10));
final ConsumerRecords<String, Object> poll2 = consumer2.poll(Duration.ofSeconds(10));
but I wonder if I can do it in one step, using one consumer.