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?