1

I want to distinguish the messages after retreiving from aws fifo sqs for routing to correct processor. And I planned to utilize the MessageGroupId , which I set while sending the request. I am using Java sdk. sendMessageRequest.setMessageGroupId(messageGroupId);

Documentation says it can be retrieved as request parameter of AttributeName

MessageGroupId - Returns the value provided by the sender that calls the SendMessage action. Messages with the same MessageGroupId are returned in sequence. However, I could not find the correct way/method to do so. There is no attribute MessageGroupId present and size is 0.

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

for (Message message : messages) 
{  
System.out.println("    Size getAttributes:          " +   message.getAttributes() .size());
}

Only if I set any attribute explicitly then it is retreivable (like with MESSAGEPRIORITY), but how to receive the values of attributes like MessageGroupId?

Help is appreciated.

Community
  • 1
  • 1
Surbhi
  • 21
  • 5
  • works with message.getAttributes() after receiveMessageRequest.withAttributeNames("All"), as suggested in below reply. – Surbhi Mar 19 '17 at 15:44

1 Answers1

3

You have to tell SQS which attributes you want it to give you, before getAttributes() will be able to show them to you.

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

Note that SQS messages have two different kinds of attributes. Attributes are system-generated (like MessageGroupId), while Message Attributes are user-generated, custom key/value pairs included when the message is sent but transported external to the message body itself.

Michael - sqlbot
  • 169,571
  • 25
  • 353
  • 427