For this, we need to implement a callback function. This function is implemented for asynchronously handling the request completion. That's why its return type will be void. This function will be implemented in the block where the producer sends data to Kafka.
The callback function invoked by the producer is onCompletion(). Basically, this method requires two arguments:
Metadata of the Record: Metadata of the record e.g. information regarding the partition and its offsets.
Exception: Any exception thrown during the produce request
Let's see the implementation of the Producer callback in the below code extrac
https://www.conduktor.io/kafka/complete-kafka-producer-with-java/