forked from oslabs-beta/kafka-penguin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exampleIgnoreProducer.ts
51 lines (46 loc) · 1.4 KB
/
exampleIgnoreProducer.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 { Ignore } from './kafka-penguin/src/index'
const producerClientIgnore = require('./clientConfig.ts');
// This example simulates an error where the producer sends to a bad topic
const topicGood = 'test-topic';
const topicBad = 'topic-non-existent';
// Set up the Ignore strategy
// Configure it with a configured KafkaJS client, a topic, and a callback that returns boolean
const exampleIgnoreProducer = new Ignore(producerClientIgnore, topicGood, true);
// Initialize a producer from the new instance of the Ignore strategy
const producerIgnore = exampleIgnoreProducer.producer();
// Connecting the producer and send messages.
// If an error occurs with a message, the strategy ignores erroneous message and continues
// publishing good messages to the topic
producerIgnore.connect()
.then(() => producerIgnore.send({
topic: topicGood,
messages: [
{
key: 'message 1',
value: JSON.stringify('Good Message'),
},
],
}))
.then(() => producerIgnore.send({
topic: topicBad,
messages: [
{
key: 'message 2',
value: 'Bad Message',
},
],
}))
.then(() => producerIgnore.send({
topic: topicGood,
messages: [
{
key: 'message 3',
value: JSON.stringify('Good Message'),
},
],
}))
.then(() => producerIgnore.disconnect())
.catch((e: any) => {
console.log(e);
});