forked from oslabs-beta/kafka-penguin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exampleFailFast.ts
29 lines (24 loc) · 911 Bytes
/
exampleFailFast.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
/* eslint-disable no-console */
import { FailFast } from './kafka-penguin/src/index';
const FailFastClient = require('../src/clientConfig.ts');
// Set up Fail Fast with the number of retried and a configured KafkaJS client
const exampleFailFast = new FailFast(2, FailFastClient);
// Initialize a producer from the new instance of Fail Fast
const producer = exampleFailFast.producer();
// Example error of a producer sending to a non-existent topic
const message = {
topic: 'topic-non-existent',
messages: [
{
key: 'hello',
value: 'world',
},
],
};
// Fail Fast will attempt to send the message to the Kafka cluster.
// After the retry count is reached, the producer will automatically disconnect.
// An Error is also thrown.
producer.connect()
.then(() => console.log('Connected!'))
.then(() => producer.send(message))
.catch((e: any) => console.log('error: ', e.message));