-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
exampleDLQProducer.ts
51 lines (46 loc) · 1.42 KB
/
exampleDLQProducer.ts
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
49
50
51
/* eslint-disable no-console */
import { DeadLetterQueue } from 'kafka-penguin';
const producerClientDLQ = require('./clientConfig.ts');
// This example simulates an error where the producer sends to a bad topic
const topicGood = 'test-topic-DLQ';
const topicBad = 'topic-non-existent';
// Set up the Dead Letter Queue (DLQ) strategy
// Configure it with a configured KafkaJS client, a topic, and a callback that returns boolean
const exampleDLQProducer = new DeadLetterQueue(producerClientDLQ, topicGood, true);
// Initialize a producer from the new instance of the Dead Letter Queue strategy
const producerDLQ = exampleDLQProducer.producer();
// Connecting the producer creates a DLQ topic in case of bad messages
// If an error occurs, the strategy moves the message to the topic specific DLQ
// The producer is able to keep publishing good messages to the topic
producerDLQ.connect()
.then(() => producerDLQ.send({
topic: topicGood,
messages: [
{
key: 'message 1',
value: 'Good Message',
},
],
}))
.then(() => producerDLQ.send({
topic: topicBad,
messages: [
{
key: 'message 2',
value: 'Bad Message',
},
],
}))
.then(() => producerDLQ.send({
topic: topicGood,
messages: [
{
key: 'message 3',
value: 'Good Message',
},
],
}))
.then(() => producerDLQ.disconnect())
.catch((e: any) => {
console.log(e);
});