发送10条以上的消息

发送10条以上的消息

本文介绍了如何使用SQS sendMessageBatch()发送10条以上的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当时在想用那段代码将所有消息发送到我的SQS队列,但是当我在数组上使用10个以上的值时,我总是收到错误消息,提示已达到限制.

I was thinking with that piece of code they are sending all the messages to my SQS queue, but when using more than 10 values on my array, I always get the error telling that exceed the limit is reached.

我无法提高SQS,因此必须将消息从10发送到10.

I can't increase my SQS, so I have to send the messages from 10 to 10.

有人从我需要针对特定​​案例的最佳方法中提出建议吗?

Anyone have a suggestion from the best approach I need to have from that particular case?

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();
}

推荐答案

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

The SendMessageBatch - Amazon Simple Queue Service documentation says you can only send up to 10 messages at one time so you will have to write your own logic. Basically split the array in batch of 10 and then send request in a loop. Something like this:

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

There is a package which supports sending bulk messages sequentially and parallely in case you want to go via that route. sqs-bulk-loader

这篇关于如何使用SQS sendMessageBatch()发送10条以上的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 05:23