Envío de mensajes a las colas de Azure Service Bus (JavaScript) y recepción desde allí

En este tutorial aprenderá a usar el paquete @azure/service-bus de un programa de JavaScript para enviar mensajes a una cola de Service Bus, y recibirlos de ella.

Requisitos previos

Nota

Uso del Administrador de paquetes para Node (NPM) para instalar el paquete

Para instalar el paquete de npm para Service Bus, abra un símbolo del sistema que tenga npm en la ruta de acceso, cambie el directorio a la carpeta en la que quiere colocar los ejemplos y ejecute este comando.

npm install @azure/service-bus

mensajes a una cola

El código de ejemplo siguiente muestra cómo enviar un mensaje a una cola.

  1. Abra el editor que prefiera, como Visual Studio Code.

  2. Cree un archivo denominado send.js y pegue en él el código siguiente. Este código envía los nombres de los científicos como mensajes a la cola.

    const { ServiceBusClient } = require("@azure/service-bus");
    
    // connection string to your Service Bus namespace
    const connectionString = "<CONNECTION STRING TO SERVICE BUS NAMESPACE>"
    
    // name of the queue
    const queueName = "<QUEUE 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 topic.
        const sender = sbClient.createSender(queueName);
    
        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 array            
    
                // 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 queue
            await sender.sendMessages(batch);
    
            console.log(`Sent a batch of messages to the queue: ${queueName}`);
    
            // 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);
     });
    
  3. Reemplace <CONNECTION STRING TO SERVICE BUS NAMESPACE> por la cadena de conexión del espacio de nombres de Service Bus.

  4. Reemplace <QUEUE NAME> por el nombre de la cola.

  5. Después, ejecute el comando en un símbolo del sistema para ejecutar este archivo.

    node send.js 
    
  6. Debería ver la siguiente salida.

    Sent a batch of messages to the queue: myqueue
    

mensajes de una cola

  1. Abra el editor que prefiera, como Visual Studio Code

  2. Cree un archivo denominado receive.js y pegue en él el código siguiente.

    const { delay, ServiceBusClient, ServiceBusMessage } = require("@azure/service-bus");
    
    // connection string to your Service Bus namespace
    const connectionString = "<CONNECTION STRING TO SERVICE BUS NAMESPACE>"
    
    // name of the queue
    const queueName = "<QUEUE NAME>"
    
     async function main() {
        // create a Service Bus client using the connection string to the Service Bus namespace
        const sbClient = new ServiceBusClient(connectionString);
    
        // createReceiver() can also be used to create a receiver for a subscription.
        const receiver = sbClient.createReceiver(queueName);
    
        // function to handle messages
        const myMessageHandler = async (messageReceived) => {
            console.log(`Received message: ${messageReceived.body}`);
        };
    
        // function to handle any errors
        const myErrorHandler = async (error) => {
            console.log(error);
        };
    
        // subscribe and specify the message and error handlers
        receiver.subscribe({
            processMessage: myMessageHandler,
            processError: myErrorHandler
        });
    
        // Waiting long enough before closing the sender to send messages
        await delay(20000);
    
        await receiver.close(); 
        await sbClient.close();
    }    
    // call the main function
    main().catch((err) => {
        console.log("Error occurred: ", err);
        process.exit(1);
     });
    
  3. Reemplace <CONNECTION STRING TO SERVICE BUS NAMESPACE> por la cadena de conexión del espacio de nombres de Service Bus.

  4. Reemplace <QUEUE NAME> por el nombre de la cola.

  5. Después, ejecute el comando en un símbolo del sistema para ejecutar este archivo.

    node receive.js
    
  6. Debería ver la siguiente salida.

    Received message: Albert Einstein
    Received message: Werner Heisenberg
    Received message: Marie Curie
    Received message: Steven Hawking
    Received message: Isaac Newton
    Received message: Niels Bohr
    Received message: Michael Faraday
    Received message: Galileo Galilei
    Received message: Johannes Kepler
    Received message: Nikolaus Kopernikus
    

En la página Información general del espacio de nombres de Service Bus en Azure Portal, verá el recuento de mensajes entrantes y salientes. Es posible que tenga que esperar alrededor de un minuto y luego actualizar la página para ver los valores más recientes.

Recuento de mensajes entrantes y salientes

Seleccione la cola en esta página Información general para ir a la página Cola de Service Bus. En esta página también verá el recuento de mensajes entrantes y salientes. También verá otra información, como el tamaño actual de la cola, el tamaño máximo o el recuento de mensajes activos.

Detalles de la cola

Pasos siguientes

Consulte la documentación y los ejemplos siguientes: