【问题标题】:How to use SQS sendMessageBatch() to send more than 10 messages?如何使用 SQS sendMessageBatch() 发送超过 10 条消息?
【发布时间】:2020-05-19 15:41:24
【问题描述】:

我正在考虑使用那段代码,他们将所有消息发送到我的 SQS 队列,但是当在我的数组上使用超过 10 个值时,我总是收到错误消息,表明已达到超出限制。

我无法增加我的 SQS,所以我必须将消息从 10 发送到 10。

有人对我需要从那个特定案例中获得的最佳方法提出建议吗?

module.exports.sendMessageBatch = function sendMessages(queueUrl, messages) {    
for (var i = 0; i < messages.length;) {
    var params = { 
        QueueUrl: queueUrl,
        Entries: [] 
    };
    for (var j = 0; j < 10 && i < messages.length; i++ , j++) {
        params.Entries.push({
            Id: uuid.v4(),
            MessageBody: JSON.stringify(messages[i])
        });
    }
    return sqs.sendMessageBatch(params).promise();
}

【问题讨论】:

    标签: node.js amazon-web-services amazon-sqs


    【解决方案1】:

    SendMessageBatch - Amazon Simple Queue Service 文档说您一次最多只能发送 10 条消息,因此您必须编写自己的逻辑。基本上以 10 个为一组拆分数组,然后循环发送请求。像这样的:

    const splitArray = require("split-array");
    
    async function sendMessages(queueUrl, messages) {
      const spilttedArray = splitArray(messages, 10);
      for (const arr of spilttedArray) {
        var params = {
          QueueUrl: queueUrl,
          Entries: []
        };
        for (const message of arr) {
          params.Entries.push({
            Id: uuid.v4(),
            MessageBody: JSON.stringify(message)
          });
        }
        await sqs.sendMessageBatch(params).promise();
      }
    }
    

    有一个包支持顺序和并行发送批量消息,以防您想通过该路线。 sqs-bulk-loader

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-31
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多