Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x | import { KafkaJS } from '@confluentinc/kafka-javascript';
import { Inject, Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { EVENT_SDK_OPTIONS } from '../shared/shared.const';
import { IEventSdkProducer } from '../shared/shared.type';
import { EVENT_SDK_KAFKAJS_TOKEN } from './kafkajs.provider';
import { IEventSdkEmitEvent, IEventSdkOptions } from './kafkajs.type';
@Injectable()
export class KafkaJSProducer implements IEventSdkProducer, OnModuleInit, OnModuleDestroy {
private readonly producer: KafkaJS.Producer;
constructor(
@Inject(EVENT_SDK_OPTIONS) private readonly options: IEventSdkOptions,
@Inject(EVENT_SDK_KAFKAJS_TOKEN) private readonly eventSdk: KafkaJS.Kafka,
) {
this.producer = this.eventSdk.producer(
this.options.producer
? {
kafkaJS: {
...this.options.producer,
...(this.options.client.logger && {
logger: this.options.client.logger.namespace(KafkaJSProducer.name),
}),
},
}
: undefined,
);
}
async onModuleInit() {
await this.producer.connect();
}
async onModuleDestroy() {
await this.producer.disconnect();
}
async emit<T>(payload: IEventSdkEmitEvent<T>) {
return this.producer.send({
topic: payload.topic,
messages: [
{
key: payload.key,
value: JSON.stringify(payload.data),
headers: payload.headers,
},
],
});
}
}
|