ConsumerSeekAware接口,onPartitionsAssigned方法在重新平衡时调用。因为我想在初始化和重新平衡时寻找偏移量中的特定偏移量。我可以将consumerSeekAware用于这两个目的,还是应该使用ConsumerRebalanceListener进行重新平衡。请给出简单的答案,因为我还没有关于春天卡夫卡的深度知识。如果可以,请提供示例代码。谢谢
答案 0 :(得分:3)
ConsumerSeekAware
有这种方法:
/**
* When using group management, called when partition assignments change.
* @param assignments the new assignments and their current offsets.
* @param callback the callback to perform an initial seek after assignment.
*/
void onPartitionsAssigned(Map<TopicPartition, Long> assignments, ConsumerSeekCallback callback);
从KafkaMessageListenerContainer.seekPartitions(Collection<TopicPartition> partitions, boolean idle)
调用,然后从ConsumerRebalanceListener.onPartitionsAssigned()
内部实现调用。最后一个有这个JavaDocs:
* A callback method the user can implement to provide handling of customized offsets on completion of a successful
* partition re-assignment. This method will be called after an offset re-assignment completes and before the
* consumer starts fetching data.
所以,是的,在重新平衡期间始终会调用ConsumerSeekAware.onPartitionsAssigned()
。顺便说一句,Apache Kafka没有initializing
这样的状态。总是rebalancing
- 经纪人处于等待状态,并在新消费者加入时开始重新平衡。