0
0
Kafkadevops~10 mins

Java producer client in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Java producer client
Create Properties
Create KafkaProducer
Create ProducerRecord
Send Record asynchronously
Callback on completion
Close Producer
This flow shows how a Java Kafka producer client is created, sends a message asynchronously, handles the callback, and then closes.
Execution Sample
Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer<String, String> producer = new KafkaProducer<>(props);
ProducerRecord<String, String> record = new ProducerRecord<>("my-topic", "key1", "value1");
producer.send(record, (metadata, exception) -> {
  if (exception == null) System.out.println("Sent");
});
producer.close();
This code creates a Kafka producer, sends a message with key and value to a topic, prints confirmation on success, and closes the producer.
Process Table
StepActionState ChangeOutput
1Create Properties objectProperties with bootstrap.servers=localhost:9092 and serializers
2Create KafkaProducer with propertiesKafkaProducer ready to send messages
3Create ProducerRecord with topic=my-topic, key=key1, value=value1ProducerRecord ready
4Call producer.send(record, callback)Send request queued asynchronously
5Callback triggered after sendNo state changePrints: Sent
6Call producer.close()Producer closed, resources freed
💡 Producer closed, no more messages can be sent
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 6
propsemptycontains bootstrap.servers and serializersunchangedunchangedunchangedunchanged
producernullnullKafkaProducer instanceKafkaProducer instanceKafkaProducer instanceclosed
recordnullnullnullProducerRecord instanceProducerRecord instanceProducerRecord instance
Key Moments - 3 Insights
Why does the send method not block the program until the message is sent?
Because send is asynchronous and queues the message to be sent in the background, as shown in step 4 of the execution_table.
What happens if we try to send a message after calling producer.close()?
The producer is closed and cannot send messages anymore, so sending after close will cause an error or no action, as noted in the exit_note.
Why do we provide a callback to the send method?
The callback runs when the send completes, letting us know if it succeeded or failed, as shown in step 5 where it prints 'Sent' on success.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of the producer variable after step 2?
Aclosed
Bnull
CKafkaProducer instance ready to send messages
DProducerRecord instance
💡 Hint
Check the variable_tracker row for 'producer' after Step 2
At which step does the callback print 'Sent'?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the output column in the execution_table for the callback action
If we remove the callback from send, what changes in the execution_table?
AStep 5 output would be empty, no confirmation printed
BProducer would not send messages
CProducer would close immediately
DProperties would be empty
💡 Hint
Step 5 shows the callback printing 'Sent'; without callback, no output occurs there
Concept Snapshot
Java Kafka Producer Client:
- Create Properties with bootstrap.servers and serializers
- Instantiate KafkaProducer with Properties
- Create ProducerRecord with topic, key, value
- Send record asynchronously with send(record, callback)
- Callback confirms success or failure
- Close producer to free resources
Full Transcript
This visual execution shows how a Java Kafka producer client works step-by-step. First, we create a Properties object and set the bootstrap server address and serializers. Then, we create a KafkaProducer instance using these properties. Next, we prepare a ProducerRecord with the topic name, key, and value. We send this record asynchronously using the send method, providing a callback that runs when the send completes. The callback prints 'Sent' if the message was sent successfully. Finally, we close the producer to release resources. Variables like props, producer, and record change state as the program runs. The send method does not block; it queues the message to send in the background. The callback lets us know when sending finishes. After closing the producer, no more messages can be sent.