Node JS AWS SQS仅接收一条消息

发布于 2025-01-14 05:25:27 字数 2281 浏览 1 评论 0 原文

我的 aws 消息收件箱中有 3 条消息。但当我尝试时我只收到一条消息 检索全部。我只收到最后一条消息,但其他两条消息没有显示。 这是我的代码:

const AWS = require("aws-sdk");
AWS.config.update({ region: "REGION" });

const sqs = new AWS.SQS({ apiVersion: "2012-11-05" });

const readMessage = function () {
  const params = {
    MaxNumberOfMessages: 10,
    QueueUrl: "https://sqs.us-east-1.amazonaws.com/56622448/my-queue",
  };

  sqs.receiveMessage(params, function (err, data) {
    if (err) {
      console.log("Error", err);
    } else if (data.Messages) {
       console.log(data);
    }
  });
};

readMessage();

这是输出:

{
  ResponseMetadata: { RequestId: 'a9e53cee-afc8-57c-664408f1e602' },
  Messages: [
    {
      MessageId: 'bb3e23f9-07fd-4205-9b53-a48f826',
      ReceiptHandle: 'AQEBD6rCOZlaBfCNn3nU4AEE7OlYwFJFeblTaiDoxIy8HiwsjdxZX+3SICY/YW5PI+RuFscMMh6VyExoo1i8Zo2JlbYj3t32b9CXnToYugzBqgZuxuYOTzXRAnrGwlavSL7hcLQvW6y8me1gnj65N3tPYEmcfXX5GIiQTn1yNEou3rUNff9DfkSije/0zvp33yfWfcW+RDzB2y6ND6eKHxfsP/cqmHjRaT0bE9rlXorjgh36YwVJ57e5bjUa/1dVqOf3ybXfEX/5C2eZM+T1V2JBxlguvuL1B3aHKAC+R9Pdgpdg2kmK3+bVmOxbQJKfU0s3sD9fElZJmLuLLMPb835z5hbVv44fKJVuEc7ad2uL3d1AUCbq3MKRCb38t77L4Ifa/ob3QQ==',
      MD5OfBody: '7b84813a4b4bf10f0edb9e8da7',
      Body: "Handsome Person Basic Information."
    }
  ]
}

我的预期输出:

{
      ResponseMetadata: { RequestId: 'a9e53cee-afc8-57c-664408f1e602' },
      Messages: [
        {
          MessageId: 'bb3e23f9-07fd-4205-9b53-a48f826',
          ReceiptHandle: 'AQEBD6rCOZlaBfCNn3nU4AEE7OlYwFJFeblTaiDoxIy8HiwsjdxZX+3SICY/YW5PI+RuFscMMh6VyExoo1i8Zo2JlbYj3t32b9CXnToYugzBqgZuxuYOTzXRAnrGwlavSL7hcLQvW6y8me1gnj65N3tPYEmcfXX5GIiQTn1yNEou3rUNff9DfkSije/0zvp33yfWfcW+RDzB2y6ND6eKHxfsP/cqmHjRaT0bE9rlXorjgh36YwVJ57e5bjUa/1dVqOf3ybXfEX/5C2eZM+T1V2JBxlguvuL1B3aHKAC+R9Pdgpdg2kmK3+bVmOxbQJKfU0s3sD9fElZJmLuLLMPb835z5hbVv44fKJVuEc7ad2uL3d1AUCbq3MKRCb38t77L4Ifa/ob3QQ==',
          MD5OfBody: '7b84813a4b4bf10f0edb9e8da7',
          Body: "Handsome Person Basic Information."
        },
       {
        Body: "Handsome Person Basic Information II"
       },
       {
        Body: "Handsome Person Basic Information III"
       }
      ]
    }

在消息下,我应该得到三个。但检索只是一条消息。

请注意:我只是缩短了我的期望值只是为了证明我的观点。

我怎样才能获得所有这三个消息?谢谢 !

I have 3 messages at my aws message inbox. But i am only getting one message when I'm trying
to retrieve all. I am getting only the last message, but the other two messages are not showing.
Here is my code:

const AWS = require("aws-sdk");
AWS.config.update({ region: "REGION" });

const sqs = new AWS.SQS({ apiVersion: "2012-11-05" });

const readMessage = function () {
  const params = {
    MaxNumberOfMessages: 10,
    QueueUrl: "https://sqs.us-east-1.amazonaws.com/56622448/my-queue",
  };

  sqs.receiveMessage(params, function (err, data) {
    if (err) {
      console.log("Error", err);
    } else if (data.Messages) {
       console.log(data);
    }
  });
};

readMessage();

This is the output:

{
  ResponseMetadata: { RequestId: 'a9e53cee-afc8-57c-664408f1e602' },
  Messages: [
    {
      MessageId: 'bb3e23f9-07fd-4205-9b53-a48f826',
      ReceiptHandle: 'AQEBD6rCOZlaBfCNn3nU4AEE7OlYwFJFeblTaiDoxIy8HiwsjdxZX+3SICY/YW5PI+RuFscMMh6VyExoo1i8Zo2JlbYj3t32b9CXnToYugzBqgZuxuYOTzXRAnrGwlavSL7hcLQvW6y8me1gnj65N3tPYEmcfXX5GIiQTn1yNEou3rUNff9DfkSije/0zvp33yfWfcW+RDzB2y6ND6eKHxfsP/cqmHjRaT0bE9rlXorjgh36YwVJ57e5bjUa/1dVqOf3ybXfEX/5C2eZM+T1V2JBxlguvuL1B3aHKAC+R9Pdgpdg2kmK3+bVmOxbQJKfU0s3sD9fElZJmLuLLMPb835z5hbVv44fKJVuEc7ad2uL3d1AUCbq3MKRCb38t77L4Ifa/ob3QQ==',
      MD5OfBody: '7b84813a4b4bf10f0edb9e8da7',
      Body: "Handsome Person Basic Information."
    }
  ]
}

My Expected Output:

{
      ResponseMetadata: { RequestId: 'a9e53cee-afc8-57c-664408f1e602' },
      Messages: [
        {
          MessageId: 'bb3e23f9-07fd-4205-9b53-a48f826',
          ReceiptHandle: 'AQEBD6rCOZlaBfCNn3nU4AEE7OlYwFJFeblTaiDoxIy8HiwsjdxZX+3SICY/YW5PI+RuFscMMh6VyExoo1i8Zo2JlbYj3t32b9CXnToYugzBqgZuxuYOTzXRAnrGwlavSL7hcLQvW6y8me1gnj65N3tPYEmcfXX5GIiQTn1yNEou3rUNff9DfkSije/0zvp33yfWfcW+RDzB2y6ND6eKHxfsP/cqmHjRaT0bE9rlXorjgh36YwVJ57e5bjUa/1dVqOf3ybXfEX/5C2eZM+T1V2JBxlguvuL1B3aHKAC+R9Pdgpdg2kmK3+bVmOxbQJKfU0s3sD9fElZJmLuLLMPb835z5hbVv44fKJVuEc7ad2uL3d1AUCbq3MKRCb38t77L4Ifa/ob3QQ==',
          MD5OfBody: '7b84813a4b4bf10f0edb9e8da7',
          Body: "Handsome Person Basic Information."
        },
       {
        Body: "Handsome Person Basic Information II"
       },
       {
        Body: "Handsome Person Basic Information III"
       }
      ]
    }

Under messages, i should be getting three. but retrieving is only one message.

Take note: I have just short cut the value of my expected value just to prove my point.

How would I able to get all of the three messages? Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱猫软卧 2025-01-21 05:25:27

这是 AWS SQS 的默认行为

“如果队列中的消息数量很少(少于 1,000 条),则每次 ReceiveMessage 调用您收到的消息很可能会少于您请求的消息数量。如果队列中的消息数量极少,您可能不会在特定的 ReceiveMessage 响应中收到任何消息,如果发生这种情况,请重复该请求

。 href="https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-sqs/classes/receivemessagecommand.html" rel="nofollow noreferrer">类 ReceiveMessageCommand

长轮询可能会有所帮助,但不保证您会立即收到所有消息

This is the default behavior of AWS SQS

"If the number of messages in the queue is small (fewer than 1,000), you most likely get fewer messages than you requested per ReceiveMessage call. If the number of messages in the queue is extremely small, you might not receive any messages in a particular ReceiveMessage response. If this happens, repeat the request. "

Class ReceiveMessageCommand

Long polling might help, but not guarantee you will receive all message at once

我乃一代侩神 2025-01-21 05:25:27

长轮询是解决方案。您可以通过两种方式实现:

  1. 递归调用您的“readMessage()”(我不建议使用 setInterval)
  2. 使用 npm SQS-消费者

Long polling is the solution. You can achieve by 2 ways :

  1. Recall your "readMessage()" recursively(I won't recommend to use setInterval)
  2. Use npm SQS-consumer
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文