Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 7x 7x 6x 23x 23x 23x 23x 23x 23x 22x 1x 23x | import { Kafka, Message, Producer } from "kafkajs";
import { validateEnvVariable } from "shared-utils";
const kafka = new Kafka({
clientId: "submit",
brokers: process.env.brokerString ? process.env.brokerString.split(",") : [],
retry: {
initialRetryTime: 300,
retries: 8,
},
ssl: {
rejectUnauthorized: false,
},
});
let producer: Producer;
export function getProducer() {
validateEnvVariable("brokerString");
return kafka.producer();
}
export async function produceMessage(topic: string, key: string, value: string) {
producer = producer || getProducer();
await producer.connect();
const message: Message = {
key: key,
value: value,
partition: 0,
headers: { source: "mako" },
};
console.log(
"About to send the following message to kafka\n" +
JSON.stringify({ ...message, value: JSON.parse(message.value as string) }, null, 2),
);
try {
await producer.send({
topic,
messages: [message],
});
console.log("Message sent successfully");
} catch (error) {
console.error("Error sending message:", error);
} finally {
await producer.disconnect();
}
}
|