azure service bus队列使用nodejs读取批量消息

azure service bus queue read bulk message using nodejs

我想使用 node js azure 包从 azure 服务总线队列中批量获取消息,现在我可以使用 set-interval.is 获取消息,还有其他方法可以批量获取消息。

服务总线 REST API 当前不支持接收批处理,因此此库中没有有效的实现。部分原因是 REST 实现的性能不是特别好,使用 AMQP 要好得多。如果您正在寻找性能更好的东西,我会看看这个库:

https://github.com/noodlefrenzy/node-amqp10

相关问题 github

你也可以试试这个out

可以从 ServiceBus 队列中批量查看指定数量的消息。可以使用 Node.js Azure 包中 ServiceBusService 上的 receiveQueueMessage 方法(支持一次只接收一条消息)从队列接收消息。默认情况下,消息将在读取时从队列中删除,但是当通过将可选参数 isPeekLock 设置为 true 以 peeklock 模式接收时,可以防止删除

使用Azure ServiceBus 的先决条件:

  1. Node.js Azure 包,可以使用命令安装,npm install azure
  2. 一个环境变量 AZURE_SERVICEBUS_CONNECTION_STRING,其中包含 Azure 模块连接到 ServiceBus 队列所需的信息

下面是接收 10 条消息的示例代码,

//Import the module

 var azure = require('azure');

 //Create ServiceBusService object

 var serviceBusService = azure.createServiceBusService();

 var receivedMessages = [];

 while(i<10){

    serviceBusService.receiveQueueMessage('myqueue', { isPeekLock: true }, 
    function(error, lockedMessage){

        if(!error){
           // Message received and locked    
           receivedMessages.push(lockedMessage)    
        }
    }
 });

 Console.log(receivedMessages);

尝试 @azure/service-bus package which has an api to receive messages in batches. This takes the max batch size and max time to wait as input. You will either get the number of messages you asked for or the number of messages that were fetched within the provided time. Checkout the sample that receives messages in a loop

const sbClient = new ServiceBusClient(connectionString);
const queueReceiver = sbClient.createReceiver(queueName);
const messages = await queueReceiver.receiveMessages(10);

您可以使用https://www.npmjs.com/package/@azure/service-bus

下面是创建接收者对象然后获取多条消息的示例代码:

let receivingMode = ReceiveMode.peekLock;//Or receiveAndDelete
let sbClient = ServiceBusClient.createFromConnectionString(connectionString);  
let subscriptionClient = this.sbClient.createSubscriptionClient(topicName, subscriptionName);
let receiver = this.subscriptionClient.createReceiver(receivingMode);
let messages = await this.receiver.receiveMessages(maxMessageCount, maxWaitTimeInSeconds);

例如,如果 maxMessageCount = 10 和 maxWaitTimeInSeconds = 60,这意味着函数将 return 10 条消息,一旦有 10 条消息,如果它等待 60 秒,它仍然会 return。