Bird
0
0

What is wrong with this code snippet that attempts to commit offsets asynchronously?

medium📝 Debug Q7 of 15
Kafka - Consumers
What is wrong with this code snippet that attempts to commit offsets asynchronously?
consumer.commitAsync(new OffsetCommitCallback() {
  public void onComplete(Map offsets, Exception e) {
    System.out.println("Offsets committed");
  }
});
AThe callback does not handle exceptions
BcommitAsync requires a map argument, not a callback
CThe method onComplete should be named onCommitComplete
DcommitAsync cannot be used with anonymous classes
Step-by-Step Solution
Solution:
  1. Step 1: Review commitAsync callback signature

    The onComplete method receives an Exception parameter which should be checked to handle commit failures.
  2. Step 2: Identify missing exception handling

    The code prints success message without checking if Exception e is null or not, ignoring possible commit errors.
  3. Final Answer:

    The callback does not handle exceptions -> Option A
  4. Quick Check:

    Always check exceptions in commitAsync callback [OK]
Quick Trick: Check exceptions in commitAsync callback to catch errors [OK]
Common Mistakes:
  • Ignoring exception parameter in callback
  • Misnaming callback methods
  • Assuming commitAsync needs map argument

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes