【问题标题】:Get particular message from Amazon SQS从 Amazon SQS 获取特定消息
【发布时间】:2020-05-13 22:13:49
【问题描述】:

有没有办法通过某个 id 检索消息。在这个answer 中写到这是不可能的。但是由于答案很旧,所以我再次询问它是否仍然相同。

我正在通过以下方式发送消息--

const params = {
      DelaySeconds: 0,
      MessageAttributes: {
        test: {
          DataType: 'String',
          StringValue: 'bdbdh',
        },
      },
      MessageBody: JSON.stringify({
        AccountId: '100'
      }),
      QueueUrl: 'url',
    };

    return new Promise((resolve, reject) => {
      sqs.sendMessage(params, function(err, data) {
        if (err) {
          console.log('data', err);
          reject(err);
        } else {
          console.log('data', data);
          resolve(data);
        }
      });
    });

通过以下方式检索消息--

const params = {
      MaxNumberOfMessages: 10,
      MessageAttributeNames: ["test"],
      VisibilityTimeout: 600,
      QueueUrl: 'url',
    };

    return new Promise((resolve, reject) => {
      sqs.receiveMessage(params, function(err, data) {
        if (err) {
          console.log('data', err);
          reject(err);
        } else {
          console.log('data', data);
          resolve(data);
        }
      });
    });

我也尝试通过属性名称获取消息,但没有成功。

【问题讨论】:

    标签: javascript node.js typescript amazon-sqs


    【解决方案1】:

    没有。无法从 Amazon SQS 队列中检索特定消息。您可以致电ReceiveMessage() 获取 1 到 10 条消息,但您无法选择接收哪些消息。

    您可以向消息添加消息属性(例如优先级、客户编号),但它们不能用于检索特定消息或消息子集。

    通常,消息会按顺序返回,但这不能保证。例如,一条不可见的消息然后又变为可见的消息将是无序的。此外,消息顺序会受到 Amazon SQS 使用的服务器的分布式特性的影响。

    见:Amazon SQS short and long polling - Amazon Simple Queue Service

    先进先出 (FIFO) 队列保证消息顺序,但不能让您访问特定消息。

    【讨论】:

      【解决方案2】:

      不幸的是,我认为没有办法通过 id 从 sqs 中提取。

      根据 SQS 文档,没有可以传递的参数:

      var params = {
        QueueUrl: 'STRING_VALUE', /* required */
        AttributeNames: [
          All | Policy | VisibilityTimeout | MaximumMessageSize | MessageRetentionPeriod | ApproximateNumberOfMessages | ApproximateNumberOfMessagesNotVisible | CreatedTimestamp | LastModifiedTimestamp | QueueArn | ApproximateNumberOfMessagesDelayed | DelaySeconds | ReceiveMessageWaitTimeSeconds | RedrivePolicy | FifoQueue | ContentBasedDeduplication | KmsMasterKeyId | KmsDataKeyReusePeriodSeconds,
          /* more items */
        ],
        MaxNumberOfMessages: 'NUMBER_VALUE',
        MessageAttributeNames: [
          'STRING_VALUE',
          /* more items */
        ],
        ReceiveRequestAttemptId: 'STRING_VALUE',
        VisibilityTimeout: 'NUMBER_VALUE',
        WaitTimeSeconds: 'NUMBER_VALUE'
      };
      sqs.receiveMessage(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else     console.log(data);           // successful response
      });
      

      AWS SQS docs

      【讨论】:

        猜你喜欢
        • 2014-10-10
        • 1970-01-01
        • 2014-11-10
        • 2016-03-05
        • 2015-09-09
        • 1970-01-01
        • 2021-03-30
        • 2012-06-09
        • 1970-01-01
        相关资源
        最近更新 更多