UNPKG

rocketmq-client-nodejs

Version:
222 lines 13 kB
"use strict"; /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.LitePushConsumerImpl = void 0; const definition_pb_1 = require("../../proto/apache/rocketmq/v2/definition_pb"); const Resource_1 = require("../route/Resource"); const PushConsumer_1 = require("./PushConsumer"); const LiteSubscriptionManager_1 = require("./LiteSubscriptionManager"); const FilterExpression_1 = require("./FilterExpression"); const LiteFifoConsumeService_1 = require("./LiteFifoConsumeService"); const LiteStandardConsumeService_1 = require("./LiteStandardConsumeService"); /** * Implementation of LitePushConsumer. * * <p>LitePushConsumer extends PushConsumer to provide lightweight message consumption * with reduced metadata and storage overhead. It supports dynamic subscription * management for lite topics.</p> * * <p>Key features:</p> * <ul> * <li>Dynamic lite topic subscription/unsubscription</li> * <li>Quota management for lite subscriptions</li> * <li>Reduced resource consumption compared to standard PushConsumer</li> * <li>Automatic synchronization with server settings</li> * </ul> * * <p>Note: Unlike LiteSimpleConsumer, LitePushConsumer does not perform route optimization * because it uses the assignment-based message delivery mechanism managed by the server.</p> */ class LitePushConsumerImpl extends PushConsumer_1.PushConsumer { liteSubscriptionManager; bindTopic; #messageListener; #enableFifoConsumeAccelerator; constructor(options) { // Create subscription expressions with bind topic const subscriptions = new Map(); subscriptions.set(options.bindTopic, FilterExpression_1.FilterExpression.SUB_ALL); super({ ...options, subscriptions, }); this.#messageListener = options.messageListener; this.#enableFifoConsumeAccelerator = options.enableFifoConsumeAccelerator ?? false; this.bindTopic = new Resource_1.Resource(options.namespace, options.bindTopic); const groupResource = new Resource_1.Resource(options.namespace, options.consumerGroup); this.liteSubscriptionManager = new LiteSubscriptionManager_1.LiteSubscriptionManager(this, this.bindTopic, groupResource); } /** * Create the consume service for lite push consumer. * * <p>Lite push consumer uses lite-topic-specific consume services to ensure * correct grouping and retry behavior.</p> */ createConsumeService() { if (!this.#messageListener) { throw new Error('messageListener should not be null'); } if (this.getPushConsumerSettings().isFifo()) { this.logger.info('Create lite FIFO consume service, consumerGroup=%s, clientId=%s, enableFifoConsumeAccelerator=%s', this.consumerGroup, this.clientId, this.#enableFifoConsumeAccelerator); return new LiteFifoConsumeService_1.LiteFifoConsumeService(this.clientId, this.#messageListener, this.#enableFifoConsumeAccelerator); } this.logger.info('Create lite standard consume service, consumerGroup=%s, clientId=%s', this.consumerGroup, this.clientId); return new LiteStandardConsumeService_1.LiteStandardConsumeService(this.clientId, this.#messageListener); } /** * Get the client type. * * @return The client type identifier for lite push consumer */ getClientType() { return definition_pb_1.ClientType.LITE_PUSH_CONSUMER; } /** * Override to disable standard assignment scanning for lite consumers. * LitePushConsumer uses Lite Subscription mechanism instead of assignments. */ // eslint-disable-next-line @typescript-eslint/no-unused-vars onTopicRouteDataUpdate(_topic, _topicRouteData) { // Lite consumers do not use standard assignment mechanism // They rely on LiteSubscriptionManager for message delivery } /** * Start up the consumer. * * <p>This method initializes the consumer and starts the lite subscription manager. * It must be called before the consumer can receive messages.</p> */ async startup() { await super.startup(); this.liteSubscriptionManager.startUp(); } /** * Shutdown the consumer. * * <p>This method gracefully shuts down the consumer, releasing all resources * and stopping the lite subscription manager.</p> */ async shutdown() { this.liteSubscriptionManager.shutdown(); await super.shutdown(); } async subscribeLite(liteTopic, offsetOption) { if (!liteTopic || liteTopic.trim().length === 0) { throw new Error('liteTopic should not be blank'); } await this.liteSubscriptionManager.subscribeLite(liteTopic, offsetOption ?? null); } /** * Unsubscribe from a lite topic. * * <p>This method removes the subscription and notifies the server. * After unsubscribing, the consumer will no longer receive messages from this topic.</p> * * @param liteTopic - The name of the lite topic to unsubscribe from */ async unsubscribeLite(liteTopic) { if (!liteTopic || liteTopic.trim().length === 0) { throw new Error('liteTopic should not be blank'); } await this.liteSubscriptionManager.unsubscribeLite(liteTopic); } /** * Get the lite topic set. * * <p>Returns an immutable set of currently subscribed lite topics. * This set reflects the current state of subscriptions managed by the lite subscription manager.</p> * * @return Set of lite topic names */ getLiteTopicSet() { return this.liteSubscriptionManager.getLiteTopicSet(); } /** * Get the load balancing group for the consumer. * * <p>The consumer group is used for load balancing across multiple consumer instances. * All consumers in the same group will share the message load.</p> * * @return Consumer group name */ getConsumerGroup() { return this.liteSubscriptionManager.getConsumerGroupName(); } /** * Handle notify unsubscribe lite command from server. * * <p>This method is called when the server sends a notification to unsubscribe * from a lite topic, typically due to quota violations or administrative actions.</p> * * @param command - The unsubscribe command from the server */ onNotifyUnsubscribeLiteCommand(command) { this.liteSubscriptionManager.onNotifyUnsubscribeLiteCommand(command); } /** * Handle settings command from server. * * <p>This method processes configuration updates from the server and synchronizes * the lite subscription manager with the latest settings.</p> * * @param endpoints - The server endpoints * @param settings - The settings configuration */ onSettingsCommand(endpoints, settings) { super.onSettingsCommand(endpoints, settings); this.liteSubscriptionManager.sync(settings); } /** * Close the consumer. */ async close() { await this.shutdown(); } /** * Get the logger. */ getLogger() { return this.logger; } /** * Get the RPC client manager (protected access for internal use). * * @internal */ getRpcClientManager() { return this.rpcClientManager; } /** * Get the endpoints (protected access for internal use). * * @internal */ getEndpoints() { return this.endpoints; } /** * Get the request timeout (protected access for internal use). * * @internal */ getRequestTimeout() { return this.requestTimeout; } } exports.LitePushConsumerImpl = LitePushConsumerImpl; //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiTGl0ZVB1c2hDb25zdW1lckltcGwuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvY29uc3VtZXIvTGl0ZVB1c2hDb25zdW1lckltcG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