-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer_consumer_no_SR.js
113 lines (96 loc) · 2.7 KB
/
producer_consumer_no_SR.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const { Kafka, CompressionTypes, logLevel } = require('kafkajs')
// This creates a client instance that is configured to connect to the Kafka broker provided by
// the environment variable KAFKA_BOOTSTRAP_SERVER
const kafka = new Kafka({
clientId: 'example-consumer',
brokers: ['xxx.eu-west-2.aws.confluent.cloud:9092'],
ssl: true,
sasl: {
mechanism: 'plain',
username: 'xxx',
password: 'xxx'
},
//logLevel: logLevel.DEBUG
})
const producer = kafka.producer()
producer.on('producer.connect', () => {
console.log(`KafkaProvider: connected`);
});
producer.on('producer.disconnect', () => {
console.log(`KafkaProvider: could not connect`);
});
producer.on('producer.network.request_timeout', (payload) => {
console.log(`KafkaProvider: request timeout ${payload.clientId}`);
});
const run = async () => {
// Producing
await producer.connect()
await producer.send({
topic: 'topickafkajsnotavro',
messages: [
{
value: Buffer.from(JSON.stringify(
{
"event_name": "QA",
"external_id": "5a12cba8",
"payload": {
"supplier_id": "dsfsdf",
"assessment": {
"performance": 7,
"quality": 7,
"communication": 7,
"flexibility": 7,
"cost": 7,
"delivery": 6
}
},
"metadata": {
"user_uuid": "5a12cba8-f4b5-495b-80ea-d0dd5d4ee17e"
}
}
))
},
],
})
// Consuming
const topic = 'topickafkajsnotavro'
const consumer = kafka.consumer({ groupId: 'test-group' })
const run = async () => {
await consumer.connect()
await consumer.subscribe({ topic, fromBeginning: true })
await consumer.run({
// eachBatch: async ({ batch }) => {
// console.log(batch)
// },
eachMessage: async ({ topic, partition, message }) => {
const prefix = `${topic}[${partition} | ${message.offset}] / ${message.timestamp}`
console.log(`- ${prefix} ${message.key}#${message.value}`)
},
})
}
run().catch(e => console.error(`[example/consumer] ${e.message}`, e))
const errorTypes = ['unhandledRejection', 'uncaughtException']
const signalTraps = ['SIGTERM', 'SIGINT', 'SIGUSR2']
errorTypes.forEach(type => {
process.on(type, async e => {
try {
console.log(`process.on ${type}`)
console.error(e)
await consumer.disconnect()
process.exit(0)
} catch (_) {
process.exit(1)
}
})
})
signalTraps.forEach(type => {
process.once(type, async () => {
try {
await consumer.disconnect()
} finally {
process.kill(process.pid, type)
}
})
})
}
run().catch(console.error)