我在SQS中有多條消息。以下代碼總是只返回一個,即使有幾十個可見(不在飛行中)。 setMaxNumberOfMessages我以爲會允許多個消費立即..我誤解了這一點?從SQS中檢索多條消息
CreateQueueRequest createQueueRequest = new CreateQueueRequest().withQueueName(queueName);
String queueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl);
receiveMessageRequest.setMaxNumberOfMessages(10);
List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
for (Message message : messages) {
// i'm a message from SQS
}
我使用withMaxNumberOfMessages沒有任何這樣的運氣也試過:
receiveMessageRequest.withMaxNumberOfMessages(10);
我怎麼知道有消息在隊列中?超過1?
Set<String> attrs = new HashSet<String>();
attrs.add("ApproximateNumberOfMessages");
CreateQueueRequest createQueueRequest = new CreateQueueRequest().withQueueName(queueName);
GetQueueAttributesRequest a = new GetQueueAttributesRequest().withQueueUrl(sqs.createQueue(createQueueRequest).getQueueUrl()).withAttributeNames(attrs);
Map<String,String> result = sqs.getQueueAttributes(a).getAttributes();
int num = Integer.parseInt(result.get("ApproximateNumberOfMessages"));
上面總是先運行,並給了我一個int
是> 1個
感謝您的輸入
您參考鏈接現在不工作,請你更新呢?我想這是http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/sqs/AmazonSQSClient.html#receiveMessage(java.lang.String) – coderz 2015-04-27 09:40:51