Bird
0
0

Identify the error in this Kafka producer code snippet:

medium📝 Debug Q6 of 15
Kafka - Event-Driven Architecture
Identify the error in this Kafka producer code snippet:
Producer producer = new KafkaProducer<>(props);
producer.send("topic", "message");
producer.close();
Asend() method requires a ProducerRecord, not separate topic and message
BKafkaProducer cannot be closed
CProducer must be created with a different constructor
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Check send() method signature

    send() expects a ProducerRecord object, not separate topic and message strings.
  2. Step 2: Identify correct usage

    Correct call is send(new ProducerRecord<>("topic", "message"));
  3. Final Answer:

    send() method requires a ProducerRecord, not separate topic and message -> Option A
  4. Quick Check:

    send() needs ProducerRecord object [OK]
Quick Trick: Always wrap topic and message in ProducerRecord for send() [OK]
Common Mistakes:
  • Passing topic and message as separate arguments
  • Trying to close producer incorrectly
  • Using wrong constructor for KafkaProducer

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes