I am trying to read kafka messages from producer using Java Multithreading.
Suppose, Kafka Producer send multiple messages to Kafka Consumer. then how to read those multiple messages seperatly using ExecutorService in JAVA
I am trying to read kafka messages from producer using Java Multithreading.
Suppose, Kafka Producer send multiple messages to Kafka Consumer. then how to read those multiple messages seperatly using ExecutorService in JAVA
I have implemented your mentioned case and i am going to share steps that you should follow.
Create a consumer class which must implement Runnable interface, it must have a Kafkaconsumer instance as a class member. You can configure consumer properties in constructor method.
public LogConsumer(List<String> topics, String group, String brokerList) {
Properties propsConsumer = new Properties();
propsConsumer.put("bootstrap.servers", brokerList);
propsConsumer.put("group.id", group);
propsConsumer.put("enable.auto.commit", "false");
propsConsumer.put("key.deserializer", StringDeserializer.class);
propsConsumer.put("value.deserializer", ByteArrayDeserializer.class);
propsConsumer.put("auto.offset.reset", "latest");
this.consumer = new KafkaConsumer(propsConsumer);
this.consumer.subscribe(topics);
}
Then, in run method you can consume each kafka message as shared below.
public void run() {
try {
while (!flagOfThread) {
ConsumerRecords<String, byte []> records = consumer.poll(10000);
for (ConsumerRecord<String, byte []> record : records) {
handleRecord(record);
}
// At least one
consumer.commitSync();
}
} catch (WakeupException e) {
// Ignore exception if closing
if (!flagOfThread){
LOG.error("Log Consumer is shutting down.",e);
}
} finally {
consumer.close();
}
}
In your application runner class, you should create a threadpool, this thread pool count must be equal to topic partition count .
ExecutorService executorService = Executors.newFixedThreadPool(parallelismCount);
for (int i = 0; i < parallelismCount; i++) {
ExecutionLogConsumer bean = new LogConsumer(/*parameters*/);
executorService.execute(bean);
}
Now you can start to consume your messages from kafka :)