Building Data Streaming Applications with Apache Kafka
by Chanchal Singh, Manisha Sethi, Manish Kumar, Anshul Joshi
Rebalance listeners
We discussed earlier that in case of addition or removal of consumer to the consumer group, Kafka triggers the rebalancer and consumer loses the ownership of the current partition. This may lead to duplicate processing when the partition is reassigned to consumer. There are some other operations such as database connection operation, file operation, or caching operations that may be part of consumer; you may want to deal with this before ownership of the partition is lost.
Kafka provides you with an API to handle such scenarios. It provides the ConsumerRebalanceListener interface that contains the onPartitionsRevoked() and onPartitionsAssigned() methods. We can implement these two methods and pass an object while subscribing ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access