In previous post we have seen the messages are sending synchronously, in case we want to send 100 messages. Let's consider, each message might take 500ms, then 100*500 becomes 50000 in terms of seconds its 50seconds. Considering 50seconds is very high which will cause hell lot of problems in case of the performance factors.
To avoid this kind of performance issues, we have a mechanism in Kafka called as Asynchronous messaging. We send a messages to Kafka broker and no need to wait for the result or response from the Kafka continue to do the other stuff.
You might be having a question in mind how we can get the response from the Kafka, this where I am coming. Please look into the below lines of code, you might understand more on the same
From the above send method , we have another argument named as a callback. callback(org.apache.kafka.clients.producer.Callback) is an interface of the kafka, which contains only one method, i.e., onCompletion(RecordMetadata,Exception).
onCompletion method having two arguments one is RecordMetadata and another is Exception. So, if we implement the org.apache.kafka.clients.producer.Callback interface will be enough to get the record metadata and also Exception as well.
Exception is useful in case of the message is not properly reached to Kafka due to many reasons. And we can get the recordmetadata only in case of there is no exceptions for the same and vice versa.