Reputation: 2661
I am trying to learn Amazon SQS. I am sending multiple message requests to my SQS like
sendMessage(sqs, qUrl, "message 1");
sendMessage(sqs, qUrl, "message 2");
sendMessage(sqs, qUrl, "message 3");
sendMessage(sqs, qUrl, "message 4");
sendMessage(sqs, qUrl, "message 5");
Note: In the parameters I pass, sqs and qUrl are all the same.
//sendMessage method:
public void sendMessage(AmazonSQS sqs, String queueUrl, String msg){
SendMessageRequest smr = new SendMessageRequest(queueUrl, msg);
sqs.sendMessage(smr);
}
But still when I try to count the numberOfRequests in the queue, it is 1 and not 5.
public int countRequests(AmazonSQS sqs, String queueUrl){
// Receive messages
int numberOfMessages=0;
System.out.println("Receiving messages");
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl);
messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
for (Message message : messages) {
numberOfMessages++;
System.out.println(" Message");
System.out.println(" MessageId: " + message.getMessageId());
System.out.println(" ReceiptHandle: " + message.getReceiptHandle());
System.out.println(" MD5OfBody: " + message.getMD5OfBody());
System.out.println(" Body: " + message.getBody());
for (Entry<String, String> entry : message.getAttributes().entrySet()) {
System.out.println(" Attribute");
System.out.println(" Name: " + entry.getKey());
System.out.println(" Value: " + entry.getValue());
}
}
return numberOfMessages;
}
Here,
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl);
messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
"messages" receive only the last message and not the first 4.
My understanding is that I should be having 5 message requests in my queue. What am I missing out? Or Have I understood wrongly? Please correct me.
Upvotes: 1
Views: 12488
Reputation: 1024
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({
region: 'REGION'
});
// Create the SQS service object
var sqs = new AWS.SQS({
apiVersion: '2012-11-05'
});
// Set params
var params = {
QueueUrl: queueURL,
AttributeNames : ['ApproximateNumberOfMessages'],
};
sqs.getQueueAttributes(queParams, function(err, data){
if (err) {
console.log("Error", err);
} else {
console.log(data);
}
});
Upvotes: 0
Reputation: 388
Below gives the total count:
['ApproximateNumberOfMessages']+['ApproximateNumberOfMessagesDelayed']+['ApproximateNumberOfMessagesNotVisible'];
you can get this from $messageData->get('Attributes') array in PHP
Upvotes: 0
Reputation: 2867
By default ReceiveMessage will only retrieve a single message. You can pass in the MaxNumberOfMessages option to retrieve more than one message in a single call.
However, be aware that even if you've written five messages to the queue, you might not get all of them back with a single ReceiveMessage call even if you set MaxNumberOfMessages to the maximum value of 10. This is because of the way SQS achieves guaranteed delivery - the system will actually store many copies of your message across many servers to ensure that your message will still get through even if a server goes down. Each message you post has to be replicated within the SQS system across many servers, and that replication is not instantaneous. This is why messages can arrive out of order, why ApproximateNumberOfMessages is approximate, and why receiveMessage may not always return as many messages as you expect. This is counter-intuitive at first, but doesn't matter in practice for many use-cases if you design your system appropriate, and it's how SQS is able to provide such high availability and scalability.
If you call receiveMessage repeatedly, you will receive all of your messages.
Upvotes: 10
Reputation: 18158
The number of messages returned by receiveMessage
is nondeterministic - to get a more reliable count on the queue size, use getQueueAttributes
and ask for the ApproximateNumberOfMessages
. Insert a short delay before this call in order to give the queue time to process the messages you sent in.
Upvotes: 8