For the latest stable version, please use Spring for Apache Kafka 3.3.0!

Using KafkaTemplate to Receive

This section covers how to use KafkaTemplate to receive messages.

Starting with version 2.8, the template has four receive() methods:

ConsumerRecord<K, V> receive(String topic, int partition, long offset);

ConsumerRecord<K, V> receive(String topic, int partition, long offset, Duration pollTimeout);

ConsumerRecords<K, V> receive(Collection<TopicPartitionOffset> requested);

ConsumerRecords<K, V> receive(Collection<TopicPartitionOffset> requested, Duration pollTimeout);

As you can see, you need to know the partition and offset of the record(s) you need to retrieve; a new Consumer is created (and closed) for each operation.

With the last two methods, each record is retrieved individually and the results assembled into a ConsumerRecords object. When creating the TopicPartitionOffsets for the request, only positive, absolute offsets are supported.