0

I want to have consumer pool and reuse each consumer. Is it correct?

KafkaConsumer consumer = createConsumer();
TopicPartition topicPartition = new TopicPartition("topic", 2);
consumer.assign(List.of(topicPartition));
consumer.seek(topicPartition, 41);

ConsumerRecords records = consumer.poll(0);
// use records...

// reuse kafka consumer 
consumer.unsubscribe();

topicPartition = new TopicPartition("topic", 1);
consumer.assign(List.of(topicPartition));
consumer.seek(topicPartition, 8);

records = consumer.poll(0);
// use records...

Or should I create new consumer each time, when I need it?

Max
  • 1,803
  • 3
  • 25
  • 39
  • You can reuse it. One Kafka consumer can consume messages from multiple topics. See here an example: https://stackoverflow.com/questions/39568947/kafka-consumer-for-multiple-topic – Octavian R. Mar 16 '19 at 11:58
  • @OctavianR. yea, I know it. But I need reusing of kafka consumers in my way – Max Mar 16 '19 at 12:07
  • in the way how you designed your code i don't see any reason to not reuse it. Did you encounter issues by reusing the consumer? – Octavian R. Mar 16 '19 at 12:40
  • @OctavianR. nope, just asking in case. – Max Mar 16 '19 at 12:41

0 Answers0