KafkaTemplate を使用した受信
このセクションでは、KafkaTemplate を使用してメッセージを受信する方法について説明します。
バージョン 2.8 以降、テンプレートには 4 つの receive() メソッドがあります。
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. Only the record at the requested offset is returned; if there is no record at that offset (for example, it was removed by retention or compaction, or the offset is beyond the end of the partition), the first two methods return null and the last two omit it from the result.
最後の 2 つの方法では、各レコードが個別に取得され、結果が ConsumerRecords オブジェクトにアセンブルされます。リクエストの TopicPartitionOffset を作成する場合、正の絶対オフセットのみがサポートされます。