从 Amazon SQS 获取特定消息

Get particular message from Amazon SQS

有什么方法可以通过某个 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);
        }
      });
    });

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

不幸的是,我认为没有办法通过 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

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

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

一般情况下,消息会按顺序返回,但这并不能保证。例如,一条不可见然后再次可见的消息将是乱序的。此外,消息顺序受到 Amazon SQS 使用的服务器的分布式特性的影响。

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

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