Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon SQS Java SDK - cannot receive message attributes

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")); 

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."); } 

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.

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.

Thanks for any help!

like image 319
John Simoes Avatar asked Aug 03 '14 18:08

John Simoes


People also ask

How do I set message attributes in SQS?

To encode a single Amazon SQS message attributeEncode the data type: the length (4 bytes) and the UTF-8 bytes of the data type. Encode the transport type ( String or Binary ) of the value (1 byte). The logical data types String and Number use the String transport type.

How do you use SQS in Java?

A dead letter queue must be of the same type as its base queue — it must be FIFO if the base queue is FIFO, and standard if the base queue is standard. For this example, we'll use a standard queue. The first thing we need to do is to create what will become our dead letter queue: String deadLetterQueueUrl = sqs.

How do I read messages from SQS queue?

When you request messages from a queue, you can't specify which message to retrieve. Instead, you specify the maximum number of messages (up to 10) that you want to retrieve. From the Queues page, select a queue. From Queue Actions, select Send and receive messages.


Video Answer


1 Answers

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(); 

or

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

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.

like image 108
Ramesh Sen Avatar answered Sep 28 '22 22:09

Ramesh Sen