UNPKG

@dugongjs/kafkajs

Version:

79 lines (78 loc) 4.38 kB
import { Partitioners } from "kafkajs"; import { MessageChannelParticipantKafkaJs } from "../../common/message-broker/message-channel-participator-kafkajs.js"; /** * MessageProducerKafkaJs is an implementation of the IMessageProducer interface using the KafkaJs library. It provides methods to publish messages to Kafka topics, as well as to connect and disconnect the producer. */ export class MessageProducerKafkaJs extends MessageChannelParticipantKafkaJs { constructor(kafka, producerConfig) { super(); this.kafka = kafka; this.producerConfig = producerConfig; this.isConnected = false; this.producer = this.kafka.producer({ createPartitioner: Partitioners.DefaultPartitioner, ...this.producerConfig }); } /** * Publishes a single message to the specified message channel (Kafka topic). The method ensures that the producer is connected before attempting to send the message. * @param _ TransactionContext is not used in this implementation, but is reserved for other implementations such as the outbox pattern. * @param messageChannelId The ID of the message channel (Kafka topic) to which the message should be published. * @param message The message object to be published, which should conform to the KafkaJs Message format. * @returns A promise that resolves when the message is successfully published, or rejects if there is an error during publishing. */ async publishMessage(_, messageChannelId, message) { this.ensureConnected(); await this.producer.send({ topic: messageChannelId, messages: [message] }); } /** * Publishes multiple messages to the specified message channel (Kafka topic). The method ensures that the producer is connected before attempting to send the messages. * @param _ TransactionContext is not used in this implementation, but is reserved for other implementations such as the outbox pattern. * @param messageChannelId The ID of the message channel (Kafka topic) to which the messages should be published. * @param messages An array of message objects to be published, which should conform to the KafkaJs Message format. * @returns A promise that resolves when the messages are successfully published, or rejects if there is an error during publishing. */ async publishMessages(_, messageChannelId, messages) { this.ensureConnected(); if (messages.length === 0) { return; } await this.producer.send({ topic: messageChannelId, messages }); } /** * Connects the Kafka producer to the Kafka cluster. This method must be called before attempting to publish any messages. If the producer is already connected, this method will have no effect. * @returns A promise that resolves when the producer is successfully connected, or rejects if there is an error during connection. * @throws An error if the producer fails to connect to the Kafka cluster. */ async connect() { await this.producer.connect(); this.isConnected = true; } /** * Disconnects the Kafka producer from the Kafka cluster. After calling this method, the producer will no longer be able to publish messages until it is connected again. If the producer is already disconnected, this method will have no effect. * @returns A promise that resolves when the producer is successfully disconnected, or rejects if there is an error during disconnection. * @throws An error if the producer fails to disconnect from the Kafka cluster. */ async disconnect() { await this.producer.disconnect(); this.isConnected = false; } /** * Ensures that the producer is created and connected before allowing messages to be published. * @throws An error if the producer is not created or not connected, with a message indicating the required action to resolve the issue. */ ensureConnected() { if (!this.producer) { throw new Error("Producer is not created. Call create() method before publishing messages."); } if (!this.isConnected) { throw new Error("Producer is not connected. Call connect() method before publishing messages."); } } }