Question: best way to pass a callback to Producer.produce()
See original GitHub issueIf I want to invoke a callback function upon delivery of the message to Kafka, what’s the best/recommended way to handle this since Producer.produce()
doesn’t accept a callback or return a Promise, and reports deliveries via events?
I have implemented this by passing a function as the opaque
value, and then calling opaque
on the delivery-report
handler. Is this a typical pattern, or is the fire & forget pattern what others do?
Below is the snippet of code that shows what I’ve come up with:
producer.on('delivery-report', (err, report) => {
if (typeof report.opaque === 'function') {
report.opaque.call(null, err, report);
}
});
producer.on('ready', () => {
publishMessage('Awesome key', new Buffer('Awesome message'), (err) => {
if (err) {
console.log('Message failed', {err});
return;
}
console.log('Message published successfully!');
});
});
function publishMessage(messageKey, messageValue, callback) {
try {
const topicName = 'Test';
const partition = -1;
const timestamp = Date.now();
producer.produce(topicName, partition, messageValue, messageKey, timestamp, callback);
} catch (err) {
callback(err);
}
}
Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Comments:5
Top Results From Across the Web
Kafka Producer Callbacks - Javatpoint
The callback function used by the producer is the onCompletion(). Basically, this method requires two arguments: Metadata of the Record: Metadata of the...
Read more >How to build an Apache KafkaProducer application with ...
In this tutorial, learn how to build an Kafka producer application and handle responses using the Callback interface using Confluent, with step-by-step ...
Read more >Apache Kafka Callback and Acks - Learning Journal
Asynchronous Producer. In this method, we send a message and provide a call back function to receive acknowledgment. We don't wait for success...
Read more >Best way of passing callback function parameters in C++
Simply do call( std::ref( your callback ) ) . std::ref overrides operator() and forwards it to the contained object. Simularly, with template< ...
Read more >Solved: Kafka Producer Async Send with Callback - Error Ha...
Problem Statement: How do I get access to the Producer Record when I encounter an exception from my asynchronous send method returned within ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If someone is looking for a promise version of this
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.