/** * @fileoverview Kafka consumer management with dynamic topic subscription * @description Handles consumer lifecycle, message processing, and dynamic topic subscription */ import { BatchMessageHandler, BatchMessageHandlerWithManualCommit, BatchTopicMapHandler, BatchTopicMapHandlerWithManualCommit, ITopicRegistryOptions, KafkaConfig, KafkaRegisteryConsumerParams, SingleMessageHandler, SingleTopicMapHandler } from "../interface/kafka.interface"; import { KafkaConnectionManager } from "./connection-manager"; import { KafkaAdminManager } from "./admin-manager"; /** * Manages Kafka consumers with dynamic topic subscription capabilities. * * @description This class handles the complex logic of consumer management including: * - Dynamic topic subscription based on naming patterns * - Both single and batch message processing * - Consumer lifecycle management * - Topic handler registration and mapping * - Integration with topic-updates for dynamic subscription * * The consumer manager supports automatic subscription to topics that match * registered patterns, enabling dynamic scaling and topic discovery. * * @example * ```typescript * const consumer = KafkaConsumerManager.getInstance(connection, config); * consumer.setSingleTopicHandler('user-events', handleUserEvents); * consumer.setBatchTopicHandler('analytics', handleAnalytics); * await consumer.initConsumer(); * ``` */ export declare class KafkaConsumerManager { private readonly kafkaConnection; private batchTopicHandlerMap; private batchManualCommitTopicHandlerMap; private singleTopicHandlerMap; private topicHandlerMap; private singleConsumersSet; private batchConsumersSet; private batchManualCommitConsumersSet; private logger; private _config; private kafkaAdmin; private static _instance; /** * Creates a new KafkaConsumerManager instance. * * @param kafkaConnection - Kafka connection manager * @param config - Kafka configuration * @private */ constructor(kafkaConnection: KafkaConnectionManager, config: KafkaConfig); /** * Gets or creates the singleton instance of KafkaConsumerManager. * * @param connection - Kafka connection manager instance * @param config - Kafka configuration * @returns The singleton KafkaConsumerManager instance */ static getInstance(connection: KafkaConnectionManager, config: KafkaConfig): KafkaConsumerManager; /** * Sets the admin manager for topic operations. * * @param admin - Admin manager instance * * @description Creates a circular dependency with the admin manager * to enable topic listing and management operations. */ setAdmin(admin: KafkaAdminManager): void; /** * Initializes all consumers and starts message processing. * * @returns Promise that resolves when all consumers are initialized * * @description Performs the following initialization steps: * 1. Lists all existing topics from the admin manager * 2. Sets up subscriptions for topics matching registered handlers * 3. Starts both single and batch consumer processing loops * * This method should be called after all handlers are registered. */ initConsumer(): Promise; private runSingleConsumer; private runBatchConsumer; private runBatchConsumerWithManualCommit; /** * Retrieves a topic handler for the specified topic. * * @param topic - Topic name to find handler for * @returns Handler configuration or undefined if not found * * @description Searches single, batch, and batch manual commit handler maps to find * a registered handler for the given topic name. */ getNewTopicHandler(topic: string): BatchTopicMapHandler | BatchTopicMapHandlerWithManualCommit | SingleTopicMapHandler | undefined; /** * Checks if a topic belongs to the current environment. * * @param topic - Full topic name to check * @returns True if topic belongs to current environment, false otherwise * @private * * @description Determines if a topic should be processed by this consumer * based on the environment prefix. Topics should start with "{env}-" to * be processed by consumers in that environment. * * Special cases: * - System topics (like 'topic-updates') are always processed * - Topics starting with the current environment prefix are processed * - All other topics are skipped * * @example * ```typescript * // Environment: 'dev' * isTopicForCurrentEnvironment('dev-user-service-123.events'); // true * isTopicForCurrentEnvironment('prod-user-service-123.events'); // false * isTopicForCurrentEnvironment('topic-updates'); // true (system topic) * ``` */ private isTopicForCurrentEnvironment; private transformedHandler; private topicUpdatedSubscription; private singleTopicSubscription; private batchTopicSubscription; private batchManualCommitTopicSubscription; private getHandler; /** * Registers a handler for single message processing. * * @param topic - Base topic name to handle * @param handler - Function to process individual messages * @param options - Optional consumer configuration * * @description Registers a handler that will be called for each individual * message received on topics matching the pattern *.{topic}. */ setSingleTopicHandler(topic: string, handler: SingleMessageHandler, options?: ITopicRegistryOptions): void; /** * Registers a handler for batch message processing. * * @param topic - Base topic name to handle * @param handler - Function to process message batches * @param options - Optional consumer configuration * * @description Registers a handler that will be called with batches of * messages received on topics matching the pattern *.{topic}. */ setBatchTopicHandler(topic: string, handler: BatchMessageHandler, options?: ITopicRegistryOptions): void; /** * Registers a handler for batch message processing with manual offset management. * * @param topic - Base topic name to handle * @param handler - Function to process message batches with manual commit control * @param options - Optional consumer configuration * * @description Registers a handler that will be called with batches of * messages received on topics matching the pattern *.{topic}. The handler * receives a resolveOffset function to manually control when offsets are committed. * This allows for more precise control over message processing acknowledgment. */ setBatchTopicHandlerWithManualCommit(topic: string, handler: BatchMessageHandlerWithManualCommit, options?: ITopicRegistryOptions): void; /** * Restarts consumer for dynamic topic subscription. * * @param topicName - Full topic name to subscribe to * @returns Promise that resolves when consumer is restarted * * @description Due to KafkaJS limitations with dynamic topic subscription, * this method creates a new consumer instance for newly discovered topics. * It handles both single and batch consumers based on the registered handler type. * * @see {@link https://github.com/tulios/kafkajs/issues/371} KafkaJS dynamic subscription issue * * @example * ```typescript * // Called automatically when topic-updates notifications are received * await consumer.restartConsumer('prod-user-service-user123.user-events'); * ``` */ restartConsumer(topicName: string): Promise; /** * Handles topic update notifications for dynamic subscription. * * @param params - Topic update event parameters * @returns Promise that resolves when topic update is processed * * @description This method is called when notifications are received on the * topic-updates topic. It triggers consumer restart for the new topic to * enable dynamic subscription without application restart. * * @example * ```typescript * // Automatically called when topic-updates receives a notification * // params.message contains the new topic name * ``` */ handleTopicUpdatesEvents: (params: KafkaRegisteryConsumerParams) => Promise; } //# sourceMappingURL=consumer-manager.d.ts.map