UNPKG

@dugongjs/kafkajs

Version:

43 lines (42 loc) 3.17 kB
import { type ILogger, type IMessageConsumer } from "@dugongjs/core"; import { Kafka, type ConsumerConfig, type ConsumerRunConfig, type ConsumerSubscribeTopics, type EachMessagePayload } from "kafkajs"; import { MessageChannelParticipantKafkaJs } from "../../common/message-broker/message-channel-participator-kafkajs.js"; /** * MessageConsumerKafkaJs is an implementation of the IMessageConsumer interface using the KafkaJs library. */ export declare class MessageConsumerKafkaJs extends MessageChannelParticipantKafkaJs implements IMessageConsumer<EachMessagePayload> { private readonly kafka; private readonly consumerConfig?; private readonly consumerSubscribeTopics?; private readonly consumerRunConfig?; private readonly logger?; private consumers; constructor(kafka: Kafka, consumerConfig?: ConsumerConfig | undefined, consumerSubscribeTopics?: ConsumerSubscribeTopics | undefined, consumerRunConfig?: ConsumerRunConfig | undefined, logger?: ILogger | undefined); /** * Generates a unique message consumer ID based on the provided origin, aggregate type, and consumer name. * @param origin The origin or source of the messages (e.g., service name or application name). * @param aggregateType The type of aggregate or domain entity that the consumer is interested in (e.g., "Order", "User", etc.). * @param consumerName A descriptive name for the consumer, which can help identify its purpose (e.g., "EmailNotificationConsumer", "AnalyticsConsumer", etc.). * @returns A unique consumer ID string in kebab-case format, combining the origin, aggregate type, and consumer name. For example: "my-service-order-email-notification-consumer". */ generateMessageConsumerIdForAggregate(origin: string, aggregateType: string, consumerName: string): string; /** * Registers a domain event message consumer for a specific channel and consumer ID, with an optional message handler. * @param channelId The ID of the message channel (Kafka topic) to subscribe to. * @param consumerId The unique ID for the consumer group. * @param onMessage An optional asynchronous function that will be called for each received message. If not provided, the consumer will run without a message handler. * @returns A promise that resolves when the consumer is successfully registered and running. */ registerDomainEventMessageConsumer(channelId: string, consumerId: string, onMessage?: (message: EachMessagePayload) => Promise<void>): Promise<void>; disconnect(): Promise<void>; /** * Subscribes to a Kafka topic with retry logic to handle the case where the topic might not exist yet. * This is required when running Kafka in KRaft mode, as topic creation is asynchronous. * @param consumer The Kafka consumer instance. * @param topic The topic to subscribe to. * @param retries The number of retry attempts before giving up. * @param delayMs The delay in milliseconds between retry attempts. * @returns A promise that resolves when the subscription is successful, or rejects if all retry attempts fail. */ private subscribeWithRetry; }