@aitickets123654/common-kafka 1.0.8 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Consumer, EachMessagePayload } from 'kafkajs';
|
|
2
2
|
import { Topics } from './topics';
|
|
3
3
|
interface Event {
|
|
4
4
|
topic: Topics;
|
|
@@ -6,12 +6,9 @@ interface Event {
|
|
|
6
6
|
}
|
|
7
7
|
export declare abstract class Listener<T extends Event> {
|
|
8
8
|
abstract topic: T['topic'];
|
|
9
|
-
abstract groupId: string;
|
|
10
9
|
abstract onMessage(data: T['data'], payload: EachMessagePayload): void;
|
|
11
|
-
protected kafka: Kafka;
|
|
12
10
|
protected consumer: Consumer;
|
|
13
|
-
constructor(
|
|
11
|
+
constructor(consumer: Consumer);
|
|
14
12
|
listen(): Promise<void>;
|
|
15
|
-
disconnect(): Promise<void>;
|
|
16
13
|
}
|
|
17
14
|
export {};
|
|
@@ -2,17 +2,16 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Listener = void 0;
|
|
4
4
|
class Listener {
|
|
5
|
-
constructor(
|
|
6
|
-
this.
|
|
5
|
+
constructor(consumer) {
|
|
6
|
+
this.consumer = consumer;
|
|
7
7
|
}
|
|
8
8
|
async listen() {
|
|
9
|
-
this.consumer = this.kafka.consumer({ groupId: this.groupId });
|
|
10
9
|
await this.consumer.connect();
|
|
11
|
-
console.log(`Listener connected to Kafka (${this.topic})`);
|
|
12
10
|
await this.consumer.subscribe({
|
|
13
11
|
topic: this.topic,
|
|
14
12
|
fromBeginning: true,
|
|
15
13
|
});
|
|
14
|
+
console.log(`Listener connected to Kafka (topic: ${this.topic})`);
|
|
16
15
|
await this.consumer.run({
|
|
17
16
|
autoCommit: false,
|
|
18
17
|
eachMessage: async (payload) => {
|
|
@@ -33,16 +32,10 @@ class Listener {
|
|
|
33
32
|
]);
|
|
34
33
|
}
|
|
35
34
|
catch (err) {
|
|
36
|
-
console.error(`Error
|
|
35
|
+
console.error(`Error in ${this.topic}:`, err);
|
|
37
36
|
}
|
|
38
37
|
},
|
|
39
38
|
});
|
|
40
39
|
}
|
|
41
|
-
async disconnect() {
|
|
42
|
-
if (this.consumer) {
|
|
43
|
-
await this.consumer.disconnect();
|
|
44
|
-
console.log(`Disconnected listener (${this.topic})`);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
40
|
}
|
|
48
41
|
exports.Listener = Listener;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Producer, Admin } from 'kafkajs';
|
|
2
2
|
import { Topics } from './topics';
|
|
3
3
|
interface Event {
|
|
4
4
|
topic: Topics;
|
|
@@ -6,12 +6,9 @@ interface Event {
|
|
|
6
6
|
}
|
|
7
7
|
export declare abstract class Publisher<T extends Event> {
|
|
8
8
|
abstract topic: T['topic'];
|
|
9
|
-
protected kafka: Kafka;
|
|
10
9
|
protected producer: Producer;
|
|
11
10
|
protected admin: Admin;
|
|
12
|
-
constructor(
|
|
13
|
-
connect(): Promise<void>;
|
|
14
|
-
disconnect(): Promise<void>;
|
|
11
|
+
protected constructor(producer: Producer, admin: Admin);
|
|
15
12
|
private ensureTopicExists;
|
|
16
13
|
publish(data: T['data']): Promise<void>;
|
|
17
14
|
}
|
|
@@ -2,21 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Publisher = void 0;
|
|
4
4
|
class Publisher {
|
|
5
|
-
constructor(
|
|
6
|
-
this.
|
|
7
|
-
this.
|
|
8
|
-
this.admin = kafka.admin();
|
|
9
|
-
}
|
|
10
|
-
async connect() {
|
|
11
|
-
await this.admin.connect();
|
|
12
|
-
await this.ensureTopicExists();
|
|
13
|
-
await this.producer.connect();
|
|
14
|
-
console.log(`Publisher connected to Kafka and topic '${this.topic}' ready`);
|
|
15
|
-
}
|
|
16
|
-
async disconnect() {
|
|
17
|
-
await this.producer.disconnect();
|
|
18
|
-
await this.admin.disconnect();
|
|
19
|
-
console.log('Kafka publisher disconnected');
|
|
5
|
+
constructor(producer, admin) {
|
|
6
|
+
this.producer = producer;
|
|
7
|
+
this.admin = admin;
|
|
20
8
|
}
|
|
21
9
|
async ensureTopicExists() {
|
|
22
10
|
try {
|
|
@@ -34,10 +22,10 @@ class Publisher {
|
|
|
34
22
|
validateOnly: false,
|
|
35
23
|
timeout: 5000,
|
|
36
24
|
});
|
|
37
|
-
console.log(`Topic '${this.topic}' created
|
|
25
|
+
console.log(`Topic '${this.topic}' created`);
|
|
38
26
|
}
|
|
39
27
|
else {
|
|
40
|
-
console.log(`Topic '${this.topic}' already exists
|
|
28
|
+
console.log(`Topic '${this.topic}' already exists`);
|
|
41
29
|
}
|
|
42
30
|
}
|
|
43
31
|
catch (err) {
|
|
@@ -46,12 +34,13 @@ class Publisher {
|
|
|
46
34
|
}
|
|
47
35
|
}
|
|
48
36
|
async publish(data) {
|
|
37
|
+
await this.ensureTopicExists();
|
|
49
38
|
try {
|
|
50
39
|
await this.producer.send({
|
|
51
40
|
topic: this.topic,
|
|
52
41
|
messages: [{ value: JSON.stringify(data) }],
|
|
53
42
|
});
|
|
54
|
-
console.log(`Event published to topic '${this.topic}'
|
|
43
|
+
console.log(`Event published to topic '${this.topic}'`, data);
|
|
55
44
|
}
|
|
56
45
|
catch (err) {
|
|
57
46
|
console.error('Failed to publish event:', err);
|