/
producer.js
34 lines (30 loc) · 1.03 KB
/
producer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const config = require("./config");
const KafkaAvro = require("./kafka_avro");
const Avro = new KafkaAvro(config, "producer");
Avro.then(avroObj => {
const producerObj = avroObj.producer;
producerObj.on("delivery-report", function(err, report) {
console.log("delivery-report: " + JSON.stringify(report), err);
});
producerObj.on("producer error", err => {
console.log(err, "producer error");
});
producerObj.on("event.log", function(log) {
// console.log("producer log:", log);
});
producerObj.on("error", function(err) {
console.log("Error from producer:", err);
});
const messageData = { idno: Math.random().toString(), name1: "Test" };
const key = { id: Math.random().toString() };
//uncomment below lines if you want delivery reports
// setInterval(() => {
// producerObj.poll();
// }, config.kafkaPollInterval);
//call this only when you want to produce message
setInterval(function() {
producerObj.produce(config.topicName, -1, messageData, key, Date.now());
}, 3000);
}).catch(err => {
console.log(Error, err);
});