node.js - 使用 nodejs 在单个请求上解决多个 Azure 服务总线消息

标签 node.js azureservicebus

我正在使用 @azure/service-bus 从 Azure 服务总线接收消息

使用这样的东西(简化)

let receivingMode = ReceiveMode.peekLock;
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);

我可能每次批量收到数百条消息,我看到我只能一条一条地完成(),例如:

for (let index = 0; index < messages.length; index++) {
    let message = messages[index];
    await message.complete();
}

这似乎是一个非常缓慢的过程......有没有更好的方法?

最佳答案

同时处理您的消息。据我了解,除非您已处理旧消息,否则您不应收到新消息。

const promises = [];
for (let index = 0; index < messages.length; index++) {
    let message = messages[index];
    promises.push(settleMessage(message));
}
await Promise.all(promises);

async function settleMessage(message) {
  // process your message here
  // Use other methods (abandon(), deadLetter(), defer()) depending on processing result;
  // Otherwise, I suggest to use different receiving mode;
  await message.complete();
}

关于node.js - 使用 nodejs 在单个请求上解决多个 Azure 服务总线消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64643484/

相关文章:

azure - 通过 Azure 服务总线进行身份验证的 WCF 数据服务

c# - Azure函数队列触发器添加Nuget包

node.js - MongoDB,有没有一种方法可以在更新文档之前添加新字段?

JavaScript - "Combining"两个相似的对象数组

node.js - create-react-app : The term 'create-react-app' is not recognized as the name of a cmdlet, 函数、脚本文件或可操作程序中存在问题

azure - 使用服务总线触发器横向扩展 Function App

c# - 如何从Azure Function更新服务总线消息?

node.js - 两级嵌套 Mongoose 模式

node.js - 如何从 Firebase 向 node.js 发送通知?

Azure服务总线,自动转发不等待消息完成