Home > Blockchain >  How to pass partitionKey or MessageId when you publish to topic in NodeJS?
How to pass partitionKey or MessageId when you publish to topic in NodeJS?

Time:01-12

I have service bus topic with Partitioning and Duplicate detection enabled. I am following enter image description here

While sending the messages in batch, I am getting following error.

Error occurred: ServiceBusError: InvalidOperationError: Message to a partitioned entity with duplicate detection enabled must have either PartitionKey or MessageId

Question:

How to pass PartitionKey or MessageId in my node app? I am using @azure/service-bus" package.

Code:

const { ServiceBusClient } = require("@azure/service-bus");

const connectionString = "<SERVICE BUS NAMESPACE CONNECTION STRING>"
const topicName = "<TOPIC NAME>";

const messages = [
    { body: "Albert Einstein" },
    { body: "Werner Heisenberg" },
    { body: "Marie Curie" },
    { body: "Steven Hawking" },
    { body: "Isaac Newton" },
    { body: "Niels Bohr" },
    { body: "Michael Faraday" },
    { body: "Galileo Galilei" },
    { body: "Johannes Kepler" },
    { body: "Nikolaus Kopernikus" }
 ];

 async function main() {
    // create a Service Bus client using the connection string to the Service Bus namespace
    const sbClient = new ServiceBusClient(connectionString);

    // createSender() can also be used to create a sender for a queue.
    const sender = sbClient.createSender(topicName);

    try {
        // Tries to send all messages in a single batch.
        // Will fail if the messages cannot fit in a batch.
        // await sender.sendMessages(messages);

        // create a batch object
        let batch = await sender.createMessageBatch(); 
        for (let i = 0; i < messages.length; i  ) {
            // for each message in the arry         

            // try to add the message to the batch
            if (!batch.tryAddMessage(messages[i])) {            
                // if it fails to add the message to the current batch
                // send the current batch as it is full
                await sender.sendMessages(batch);

                // then, create a new batch 
                batch = await sender.createMessageBatch();

                // now, add the message failed to be added to the previous batch to this batch
                if (!batch.tryAddMessage(messages[i])) {
                    // if it still can't be added to the batch, the message is probably too big to fit in a batch
                    throw new Error("Message too big to fit in a batch");
                }
            }
        }

        // Send the last created batch of messages to the topic
        await sender.sendMessages(batch);

        console.log(`Sent a batch of messages to the topic: ${topicName}`);

        // Close the sender
        await sender.close();
    } finally {
        await sbClient.close();
    }
}

// call the main function
main().catch((err) => {
    console.log("Error occurred: ", err);
    process.exit(1);
 });

CodePudding user response:

How to pass PartitionKey or MessageId in my node app?

You just need to specify the message id in messageId property and partition key in partitionKey property of the message. For example, your payload would look something like:

const messages = [
    { body: "Albert Einstein", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Werner Heisenberg", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Marie Curie", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Steven Hawking", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Isaac Newton", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Niels Bohr", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Michael Faraday", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Galileo Galilei", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Johannes Kepler", messageId: 'message-id', partitionKey: 'partition-key' },
    { body: "Nikolaus Kopernikus", messageId: 'message-id', partitionKey: 'partition-key' }
 ];

Reference: https://learn.microsoft.com/en-us/javascript/api/@azure/service-bus/servicebusmessage?view=azure-node-latest

  • Related