【问题标题】:Amazon AWS SQS Java - Attributes NullAmazon AWS SQS Java - 属性 Null
【发布时间】:2020-05-09 05:18:41
【问题描述】:

我正在尝试从正在处理的每条消息中提取 AWS SQS 消息属性。我已验证该属性存在并且所有内容都拼写正确: 名称:请求标识 值:thisistherequestid

我通过这种方式收到消息:

messages = sqs.receiveMessage(request.withQueueUrl(queueUrl).withAttributeNames("All")).getMessages()

我也试过了:

messages = sqs.receiveMessage(request.withQueueUrl(queueUrl).withAttributeNames("RequestId")).getMessages()

我已经尝试了这两件事,但每次都返回 null。

        for (Message m : messages) {
            Map<String, MessageAttributeValue> attributeValues = new HashMap<String, MessageAttributeValue>();
            attributeValues = m.getMessageAttributes();
            System.out.println("Attribute Values ===>  " + attributeValues.get("RequestId").getStringValue());

我也试过了:

        for (Message m : messages) {
            Map<String, String> attributeValues = new HashMap<String, String>();
            attributeValues = m.getAttributes();
            System.out.println("Attribute Values ===>  " + attributeValues.get("RequestId"));

我不太确定这里出了什么问题.. 即使你这样做,属性也是空的:

            attributeValues = m.getMessageAttributes();
            System.out.println("Attribute Values ===>  " + attributeValues);

知道我做错了什么吗?

这是我在 AWS 控制台中看到的:

【问题讨论】:

  • 当您说“我已验证该属性存在”时,您是什么意思?您是否通过 AWS 控制台进行了验证?
  • 是的,我可以在 AWS 控制台上看到两条消息。我可以使用 SQS SDK for Java 很好地处理它们。但无论我做什么,属性都是空的。我可以单击 Message Attributes 选项卡并验证是否存在属性。 **更新为显示消息属性选项卡。
  • 您已经确认您在代码中使用的queueUrl 与您选择队列名称时在SQS 控制台上显示的相同?您的代码对我来说看起来不错(除了您不需要分配 attributeValues - 这在下一行被丢弃)所以我正在寻找其他问题。
  • 是的......它可以毫无困难地处理和删除消息。这一切都很好。只是这些该死的消息属性。我一生都无法理解为什么会失败。如果我使用 getAttributes() 它会很好.. 但是 getMessageAttributes() 总是什么都不返回。
  • 知道了...AWS 网站上隐藏了一条评论。当您设置 ReceiveMessageRequest 对象时,您必须同时指定 withMessageAttributeNames("RequestId") 和 withAttributeNames("")... 即使您只是添加空引号的第二个对象。这是非常古怪的。 3个多小时后…………

标签: java amazon-web-services attributes amazon-sqs


【解决方案1】:

你设置的是sqs中的message属性。因此,在检索时,您必须使用以下代码。

messages = sqs.receiveMessage(request.withQueueUrl(queueUrl).withMessageAttributeNames("RequestId")).getMessages()

这将获取名称为RequestId的消息属性

【讨论】:

    猜你喜欢
    • 2014-11-10
    • 2014-09-26
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 2011-04-27
    • 2016-09-03
    相关资源
    最近更新 更多