Amazon SQS Java SDK - 无法接收消息属性 [英] Amazon SQS Java SDK - cannot receive message attributes

查看:207
本文介绍了Amazon SQS Java SDK - 无法接收消息属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发送带有以下代码的SQS属性的消息之后发送它(并检查SQS控制台以查看是否所有内容都正确发布)...

After posting a message with an attribute to SQS with the following code before sending it (and checking in SQS console to see if everything is posted correctly)...

messageRequest.addMessageAttributesEntry(
        "attributeTest", 
        new MessageAttributeValue()
            .withDataType("String")
            .withStringValue("attributeTest 123"));

我无法检索邮件中的任何邮件属性。因此,我所看到的只是0属性。在Amazon SQS控制台中重新检查消息,消息 - 和属性 - 仍然存在。

I cannot retrieve any of the message attributes in the message. All I see, as a result, is "0 attributes.". Reinspecting the message in Amazon SQS console, the message - and the attribute - are still there.

// Message was previously checked in SQS console and contains 
// an attribute named "attributeTest"

AmazonSQS sqs = ...
List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
for (Message : messages) 
{
    Map<String, MessageAttributeValue> attributes = message.getMessageAttributes();
    System.out.println("" + attributes.size() + " attributes.");
}

我在Java 1.7中使用Amazon SQS SDK v1.8和Play Framework 2.2 0.3。起初我认为它可能是SQS版本,但尝试升级到1.8.7但无济于事。

I am using Amazon SQS SDK v1.8 in Java 1.7 with Play Framework 2.2.3. At first I thought it could be the SQS version but tried upgrading to 1.8.7 with no avail.

官方文档发现这里根本没有提供任何源代码来读取属性。也没有github搜索,堆栈溢出。我已经尝试了几个小时而没有任何成功。

The official documentation found here does not provide any sourcecode to read the attributes at all. Nor github searches, stack overflow. I've been trying for a few hours without any success.

感谢您的帮助!

推荐答案

您必须在请求中指定所需的消息属性(或使用全部获取所有消息属性)。因此,在您的情况下,您可以使用

You have to specify which message attributes you want in your request (or use "All" to get all message attributes). So in your case you could use either

List<Message> messages = sqs.receiveMessage(receiveMessageRequest.withMessageAttributeNames("attributeTest")).getMessages();

List<Message> messages = sqs.receiveMessage(receiveMessageRequest.withMessageAttributeNames("All")).getMessages();

如果您想要标准属性(ApproximateFirstReceiveTimestamp,ApproximateReceiveCount,SenderId和SentTimestamp)以及消息,使用 withAttributeNames(All)。您可以在Javadocs中找到更多详细信息,以便 ReceiveMessageRequest

If you want the standard attributes (ApproximateFirstReceiveTimestamp, ApproximateReceiveCount, SenderId, and SentTimestamp) along with your messages, use withAttributeNames("All"). You can find more detail in the Javadocs for ReceiveMessageRequest.

这篇关于Amazon SQS Java SDK - 无法接收消息属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆