Polling an Azure Service Bus Queue from an Azure WebJob using Node.js

前端 未结 3 2015
闹比i
闹比i 2021-01-14 19:55

Trying to poll an Azure Service Bus Queue using a WebJob written in Node.js. I created 2 WebJobs. The first is on demand and sends 10 unique messages to the queue. The secon

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-14 20:22

    Try using the amqp to read the messages off the azure service bus partitioned queue and this will work for a partitioned topic/queue and you don't even have to poll a lot.

    const AMQPClient = require('amqp10').Client;
    const Policy = require('amqp10').Policy;
    
    const protocol = 'amqps';
    const keyName = 'RootManageSharedAccessKey';
    const sasKey = 'your_key_goes_here';
    const serviceBusHost = 'namespace.servicebus.windows.net';
    const uri = `${protocol}://${encodeURIComponent(keyName)}:${encodeURIComponent(sasKey)}@${serviceBusHost}`;
    const queueName = 'partitionedQueueName';
    const client = new AMQPClient(Policy.ServiceBusQueue);
    client.connect(uri)
    .then(() => Promise.all([client.createReceiver(queueName)]))
    .spread((receiver) => {
        console.log('--------------------------------------------------------------------------');
        receiver.on('errorReceived', (err) => {
            // check for errors
            console.log(err);
        });
        receiver.on('message', (message) => {
            console.log('Received message');
            console.log(message);
            console.log('----------------------------------------------------------------------------');
        });
    })
    .error((e) => {
        console.warn('connection error: ', e);
    });
    

    https://www.npmjs.com/package/amqp10

提交回复
热议问题