How to create a Callback for Kafka Producer
Blocking send method is suitable for the scenarios where your messages are produced
at the slower pace. Asynchronous sends are more efficient as the producer can
leverage grouping multiple messages and optimise network roundtrips to achieve
better throughput. In high throughput use cases, you can supply a call-back method
to your producer to be invoked when the acknowledgment arrives. Let’s create an
example for the same.
We want to take the Hello Producer example and implement a call back method to the producer. The call back method should log the errors and success condition.
This example is an excerpt from the Book Kafka Streams – Real-time Stream Processing
For a detailed explanation of the example and much more, you can get access to the Book using below link.
Solution - Call-back Producer
Implementing a call-back is as simple as passing a lambda function to the send() method. The code snippet shown below demonstrates an example for the same.
You can access fully function project in our GitHub folder.