Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a listener container in Spring for Apache Kafka?

I understood that to make a method to be the target of Kafka message listener, I have to mark this method with the @KafkaListener annotation. This annotation lets specify by containerFactory element the KafkaListenerContainerFactory.

Below there are some snippet by Baeldung Spring Kafka Tutorial.

KafkaConsumerConfig.java

private ConsumerFactory<String, String> consumerFactory(String groupId) {
    Map<String, Object> props = new HashMap<>();
    ...
    return new DefaultKafkaConsumerFactory<>(props);
}

private ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory(String groupId) {
    ConcurrentKafkaListenerContainerFactory<String, String> factory =
            new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory(groupId));
    return factory;
}

@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> fooKafkaListenerContainerFactory() {
    return kafkaListenerContainerFactory("foo");
}

MessageListener.java

@KafkaListener(
    topics = "${message.topic.name}", 
    groupId = "foo", 
    containerFactory = "fooKafkaListenerContainerFactory")
public void listenGroupFoo(String message) {
    System.out.println("Received Message in group 'foo': " + message);
    ...
}

What I didn't understand is why we need a factory of listeners container. What is a listener container? What happen when a method is annotated in that way?

like image 827
Vin Avatar asked Sep 22 '20 14:09

Vin


People also ask

What is Spring Kafka listener?

@KafkaListener designates a method as a listener in a KafkaMessageListenerContainer. A KafkaMessageListenerContainer is how Spring Boot connects and polls records from Kafka under the hood. Remember that the @Component annotation tells Spring Boot to register our KafkaConsumer class as a managed Spring Bean.

What does Kafka listener do?

The Kafka Listener is work on the publish and subscribe model. The Apache Kafka is nothing but a massaging protocol. The Kafka broker will receive the number of messages by the Kafka topics. We need to set the listener configuration correctly.

How does Spring Kafka listener work?

When you annotate the method, Spring takes care of instantiating the underlying containers that will run your Kafka consumers and read messages from your Kafka topics and handle serialization. All of these things are managed by Spring so you can focus on your application code.


1 Answers

A listener "container" is a Spring concept across multiple technologies (JMS, RabbitMQ, Kafka, AWS, etc, etc).

The listener is defined as a POJO bean method and is a property of the container (the container "contains" the listener).

The container is responsible for interacting with the broker to receive messages and invoke your listener method with each message, or a batch of messages, depending on the listener type.

This means your application code does not have to deal with the mechanics of interacting with the broker and you can concentrate on your business logic only.

The framework discovers any @KafkaListener methods and uses the factory to create a container for each one.

like image 198
Gary Russell Avatar answered Sep 20 '22 22:09

Gary Russell