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 to the topic using the subscribe method discussed earlier:
public class DemoRebalancer implements ConsumerRebalanceListener { @Override public void onPartitionsRevoked(Collection<TopicPartition> collection) { //TODO: Things to Do before your partition got revoked }
@Override public void onPartitionsAssigned(Collection<TopicPartition> collection) { //TODO : Things to do when new partition get assigned } }