messageProducer.ts 731 Bytes
Newer Older
chinguyen's avatar
chinguyen committed
1
import { Producer } from 'kafka-node'
2
import { getMessageProducer } from './message'
chinguyen's avatar
chinguyen committed
3

4 5 6 7 8
export function produceMessage() {
  const _that = this;
  let producer = getMessageProducer();
  let topicName = 'message-chinguyen';
  let produceTopic = topicName;
chinguyen's avatar
chinguyen committed
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
  let payloads: {
    topic: any;
    messages: any;
    key: any;
    attributes: any;
    timestamp: any;
  }[] = [];
  let curTimestamp = Date.now();
  payloads.push({ topic: produceTopic, messages: 'hahahahaha', key: 1, attributes: 'haha', timestamp: curTimestamp });

  if (payloads.length > 0) {
    producer.send(payloads, function(err, data) {
      if (err) {
        console.log("error");
      }
      else {
        console.log("success");

      }
    });
  }


}