我正在尝试学习 Amazon SQS。我正在向我的 SQS 发送多个消息请求,例如
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");
注意:在我传递的参数中,sqs和qUrl都是一样的。
//发送消息方法:
public void sendMessage(AmazonSQS sqs, String queueUrl, String msg){
SendMessageRequest smr = new SendMessageRequest(queueUrl, msg);
sqs.sendMessage(smr);
}
但是当我尝试计算队列中的 numberOfRequests 时,它仍然是 1 而不是 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;
}
在这里,
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl);
messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
“消息”只接收最后一条消息,而不是前 4 条。
我的理解是我的队列中应该有 5 个消息请求。我错过了什么?还是我理解错了?请纠正我。