Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access Amazon SQS message attributes in C#

I have a process that creates SQS messages and places them on an SQS queue and another process that reads those messages and performs certain logic based on the contents of the body and attributes of the message.

I can successfully create a message on the SQS queue with body and attributes, but I have a problem when reading the message attributes back!

I am sure my message creation process is correct, I can see the attributes in the AWS SQS console for the queue. I just cannot figure out why I cannot read those attributes back.

My code to create the message:

IAmazonSQS sqs = AWSClientFactory.CreateAmazonSQSClient();
var sendMessageRequest = new SendMessageRequest();
sendMessageRequest.QueueUrl = "myURL";
Dictionary<string, MessageAttributeValue> MessageAttributes = new Dictionary<string, MessageAttributeValue>();
MessageAttributeValue messageTypeAttribute = new MessageAttributeValue();
messageTypeAttribute.DataType = "String";
messageTypeAttribute.StringValue = "HIGH";
MessageAttributes.Add("MESSAGEPRIORITY", messageTypeAttribute);

sendMessageRequest.MessageAttributes = MessageAttributes;
sendMessageRequest.MessageBody = "Thats the message body";

sqs.SendMessage(sendMessageRequest);

The above works and a message with attribute MESSAGEPRIORITY=HIGH is created (I can see the message and attribute in the SQS Console).

When reading back the message (I've skipped parts of the code that show Queue URL etc):

IAmazonSQS sqs = AWSClientFactory.CreateAmazonSQSClient();
ReceiveMessageResponse receiveMessage = new ReceiveMessageResponse();
ReceiveMessageRequest request = new ReceiveMessageRequest();
request.QueueUrl = "myURL";
receiveMessage = sqs.ReceiveMessage(request);
string messageBody = receiveMessage.Messages[0].Body;
Dictionary<string, MessageAttributeValue> messageAttributes = receiveMessage.Messages[0].MessageAttributes;

With the above code, I get the body of the message, but the attributes are empty! I have the same problem when creating the message manually using the SQS queue directly. I can't figure out why? Any help would be great. Many thanks,

like image 389
azog Avatar asked May 09 '14 12:05

azog


People also ask

How do I view AWS SQS messages?

Amazon SQS begins to poll servers to find messages in the queue. The progress bar on the right side of the Receive messages section displays the polling duration. The Messages section displays a list of the received messages. For each message, the list displays the message ID, sent date, size, and receive count.

How do I retrieve messages from SQS?

To receive and delete a message (console)Open the Amazon SQS console at https://console.aws.amazon.com/sqs/ . In the navigation pane, choose Queues. On the Queues page, choose a queue. Choose Send and receive messages.

What is the use of message attributes in SQS?

You can use message attributes to attach custom metadata to Amazon SQS messages for your applications. You can use message system attributes to store metadata for other AWS services, such as AWS X-Ray.

What is ApproximateReceiveCount?

ApproximateReceiveCount – Returns the number of times a message has been received across all queues but not deleted.


2 Answers

Ok so I figured this one out. The attribute names need to be specified as a property of the ReceiveMessageRequest object before the call is made to pull the message.

So, the code above needs to change to:

IAmazonSQS sqs = AWSClientFactory.CreateAmazonSQSClient();
ReceiveMessageResponse receiveMessage = new ReceiveMessageResponse();
ReceiveMessageRequest request = new ReceiveMessageRequest();

//Specify attribute list
List<string> AttributesList = new List<string>();
AttributesList.Add("MESSAGEPRIORITY");

//Assign list and QueueURL to request
request.MessageAttributeNames = AttributesList;
request.QueueUrl = "myURL";

//Receive the message...
receiveMessage = sqs.ReceiveMessage(request);
//Body...
string messageBody = receiveMessage.Messages[0].Body;
//...and attributes
Dictionary<string, MessageAttributeValue> messageAttributes = receiveMessage.Messages[0].MessageAttributes;

The above works for me. Hopefully it'll be useful to someone....

like image 129
azog Avatar answered Sep 19 '22 14:09

azog


To retrieve all attributes of a message without specifying each one you can put "*" or "All" in your attributes list. Like so :

//Specify attribute list
List<string> AttributesList = new List<string>();
AttributesList.Add("*");

AWS SQS ReceiveMessage documentation

like image 39
severin.julien Avatar answered Sep 16 '22 14:09

severin.julien