Okay, there are a lot of questions here
Would like to know if it is possible to read and process multiple messages by a > Kafka consumer(Java client) parallely
The kafka client for java only supports serial processing, you can parallelize kafka consumer up to the number of partitions your topic have by creating many threads and one consumer for each one, threads are tricky though, I would suggest you to use some library to achieve that, e.g. rapids-kafka-client.
public static void main(String[] args){
ConsumerConfig.<String, String>builder()
.prop(KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName())
.prop(VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName())
.prop(GROUP_ID_CONFIG, "stocks")
.topics("stock_changed")
.consumers(7)
.callback((ctx, record) -> {
System.out.printf("status=consumed, value=%s%n", record.value());
})
.build()
.consume()
.waitFor();
}
- Is it a good approach to do so???
Yep, no problem on that, partitions were created to make parallelism possible
- And also as per my understanding Kafka treats even each thread as a consumer...please correct me if I'm wrong ...
On Kafka vanilla client library a consumer is a class capable of download messages from one or more partitions of a topic, this client don't support many threads by itself, you can create many threads or use some library (e.g. rapids-kafka-client) for that purpose so you can create many consumers for the different topic partitions and consume in parallel then.
- and also would like to make the Java client as daemon service to run in Linux so that it runs continuously and polls Kafka for messages , read and process the same ..is this a good approach ..
Yes, use the library, make your code, publish a jar, run that and let it keep processing data