Bird
0
0

How can you programmatically unsubscribe a Kafka consumer from all topics?

hard📝 Application Q9 of 15
Kafka - Consumers
How can you programmatically unsubscribe a Kafka consumer from all topics?
ACall <code>consumer.subscribe(Collections.emptyList());</code>
BCall <code>consumer.unsubscribe();</code>
CCall <code>consumer.subscribe(null);</code>
DClose the consumer without unsubscribing.
Step-by-Step Solution
Solution:
  1. Step 1: Identify the method to unsubscribe

    Kafka consumer provides an explicit unsubscribe() method to stop listening to all topics.
  2. Step 2: Check other options

    subscribe(Collections.emptyList()) is not guaranteed to unsubscribe; subscribe(null) causes error; closing consumer ends consumption but is not unsubscribing.
  3. Final Answer:

    Call consumer.unsubscribe(); -> Option B
  4. Quick Check:

    Use unsubscribe() to stop all topic subscriptions [OK]
Quick Trick: Use unsubscribe() to clear all topic subscriptions [OK]
Common Mistakes:
  • Passing empty list to subscribe() expecting unsubscribe
  • Passing null to subscribe() causing error
  • Confusing closing consumer with unsubscribing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes