Harshal Bulsara
Harshal Bulsara

Reputation: 8254

SQS : Getting Number of message in the SQS Queue

I am working with Amazon-SQS, I tried to retrieve approximate number of attributes from the queue but the response is null

I am using C# following is the code:

GetQueueAttributesRequest attReq = new GetQueueAttributesRequest();
attReq.QueueUrl = "Link to queue";
GetQueueAttributesResponse response = client.GetQueueAttributes(attReq);
Console.WriteLine("App. messages: "+ response.ApproximateNumberOfMessages);

I am getting null response from the request, I am sure there are messages in the queue as well.

Upvotes: 5

Views: 4579

Answers (1)

Anthony Neace
Anthony Neace

Reputation: 26003

You have to explicitly specify which attributes you would like to return from GetQueueAttributes. You didn't specify any, so it didn't return any.

Try simply adding ApproximateNumberOfMessages to the AttributeNames collection on GetQueueAttributesRequest:

GetQueueAttributesRequest attReq = new GetQueueAttributesRequest();
attReq.QueueUrl = "Link to queue";
attReq.AttributeNames.Add("ApproximateNumberOfMessages");
GetQueueAttributesResponse response = client.GetQueueAttributes(attReq);

Notes:

  • This property might be called AttributeName without the last s if you're on an older version of the AWSSDK. It looks like this changed between versions 1.x and 2.x.
  • Full list of attributes can be found in the API documentation

Upvotes: 11

Related Questions