Vin
Vin

Reputation: 823

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 snippets 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?

Upvotes: 22

Views: 13825

Answers (1)

Gary Russell
Gary Russell

Reputation: 174759

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.

Upvotes: 32

Related Questions