Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use ConcurrentKafkaListenerContainerFactory?

I am new to kafka and i went through the documentation but I couldn't understand anything. Can someone please explain when to use the ConcurrentKafkaListenerContainerFactory class? I have used the Kafkaconsumer class but I see ConcurrentKafkaListenerContainerFactory being used in my current project. Please explain what purpose it serves.

like image 650
Rahul Gupta Avatar asked Mar 06 '19 12:03

Rahul Gupta


People also ask

What is the use of ConcurrentKafkaListenerContainerFactory?

ConcurrentKafkaListenerContainerFactory api provides concurrent way of using Kafka Consumer API along with setting other kafka consumer properties.

What is the use of @enablekafka?

The KafkaListenerContainerFactory is responsible to create the listener container for a particular endpoint. Typical implementations, as the ConcurrentKafkaListenerContainerFactory used in the sample above, provides the necessary configuration options that are supported by the underlying MessageListenerContainer .

What is the difference between Kafka listener and consumer?

They are not interchangeable in Java code. KafkaConsumer is a class from the Kafka client library which provides the API for applications to receive messages. KafkaListener is an annotation applied to a method so Spring Kafka will invoke it to process a message.

Is KafkaTemplate send asynchronous?

Once you have your ProducerFactory bean, you can create a KafkaTemplate . Kafka is an asynchronous system by default, so its send method always returns a Future .


2 Answers

The Kafka consumer is NOT thread-safe. All network I/O happens in the thread of the application making the call. It is the responsibility of the user to ensure that multi-threaded access is properly synchronized. Un-synchronized access will result in ConcurrentModificationException.

If a consumer is assigned multiple partitions to fetch data from, it will try to consume from all of them at the same time, effectively giving these partitions the same priority for consumption. However in some cases consumers may want to first focus on fetching from some subset of the assigned partitions at full speed, and only start fetching other partitions when these partitions have few or no data to consume.

Spring-kafka

ConcurrentKafkaListenerContainerFactory is used to create containers for annotated methods with @KafkaListener

There are two MessageListenerContainer in spring kafka

KafkaMessageListenerContainer
ConcurrentMessageListenerContainer

The KafkaMessageListenerContainer receives all message from all topics or partitions on a single thread. The ConcurrentMessageListenerContainer delegates to one or more KafkaMessageListenerContainer instances to provide multi-threaded consumption.

Using ConcurrentMessageListenerContainer

@Bean
KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<Integer, String>>
                    kafkaListenerContainerFactory() {
    ConcurrentKafkaListenerContainerFactory<Integer, String> factory =
                            new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory());
    factory.setConcurrency(3);
    factory.getContainerProperties().setPollTimeout(3000);
    return factory;
  }

It has a concurrency property. For example, container.setConcurrency(3) creates three KafkaMessageListenerContainer instances.

If you have six TopicPartition instances are provided and the concurrency is 3; each container gets two partitions. For five TopicPartition instances, two containers get two partitions, and the third gets one. If the concurrency is greater than the number of TopicPartitions, the concurrency is adjusted down such that each container gets one partition.

here is the clear example with documentation here

like image 86
Deadpool Avatar answered Sep 21 '22 22:09

Deadpool


Kafka Consumer API is not thread safe. ConcurrentKafkaListenerContainerFactory api provides concurrent way of using Kafka Consumer API along with setting other kafka consumer properties.

like image 22
Rohit Yadav Avatar answered Sep 19 '22 22:09

Rohit Yadav