Search code examples
springkafka-consumer-apispring-kafka

Kafka listener receiving List<ConsumerRecord<String, String>>, is it possible to consume?


I am super new in Kafka and I frankly have no idea about this type of consumer (as far as I understood is like that due is batch ready), so I am struggling to figure out how to basically consume the list of these events.

I have something like this:

    @KafkaListener(topics = "#{'${kafka.listener.list-of-topics}'.split(',')}")
        public void readMessage(List<ConsumerRecord<String, String>> records,
                                   final Acknowledgment acknowledgment) {

            try {
....

I know when I receive an event (at least a single one) is of type "MyObject" so I can do it fine when I get a single message.

I believe there must be a way to read/cast this List<ConsumerRecords<String,String> but I cannot figure out how..

any ideas?


Solution

  • See the reference manual: Batch Listeners.

    Starting with version 1.1, @KafkaListener methods can be configured to receive the entire batch of consumer records received from the consumer poll. To configure the listener container factory to create batch listeners, set the batchListener property:

    @Bean
    public KafkaListenerContainerFactory<?> batchFactory() {
        ConcurrentKafkaListenerContainerFactory<Integer, String> factory =
                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        factory.setBatchListener(true);  // <<<<<<<<<<<<<<<<<<<<<<<<<
        return factory;
    }
    

    ...

    You can also receive a list of ConsumerRecord<?, ?> objects but it must be the only parameter (aside from optional Acknowledgment, when using manual commits, and/or Consumer<?, ?> parameters) defined on the method:

    ...

    When using Spring Boot, set the property spring.kafka.listener.type=batch.