检索SQS多封邮件 [英] Retrieve multiple messages from SQS

查看:199
本文介绍了检索SQS多封邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQS多条消息。下面code 总是只有一个返回,即使有几十个可见的(不是在飞行中)。 <一href="https://github.com/amazonwebservices/aws-sdk-for-java/blob/master/src/main/java/com/amazonaws/services/sqs/model/ReceiveMessageRequest.java#L195">setMaxNumberOfMessages我以为会允许多一次要消耗..我有误解呢?

I have multiple messages in SQS. The following code always returns only one, even if there are dozens visible (not in flight). setMaxNumberOfMessages I thought would allow multiple to be consumed at once .. have i misunderstood this?

 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
 }

我也试着使用<一个href="https://github.com/amazonwebservices/aws-sdk-for-java/blob/master/src/main/java/com/amazonaws/services/sqs/model/ReceiveMessageRequest.java#L208">withMaxNumberOfMessages没有任何这样的运气:

I've also tried using withMaxNumberOfMessages without any such luck:

 receiveMessageRequest.withMaxNumberOfMessages(10);

我怎么知道有队列中的消息? 1个多?

How do I know there are messages in the queue? More than 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

感谢您的输入

推荐答案

<一个href="http://docs.amazonwebservices.com/AWSSimpleQueueService/latest/APIReference/Query_QueryReceiveMessage.html?r=8109">AWS API参考指南:查询/ QueryReceiveMessage

由于队列的分布式特性,加权随机组机器上采样一个ReceiveMessage呼叫。这意味着只对采样的机器中的消息被返回。如果消息队列中的数量少(少于1000),它很可能比你每ReceiveMessage呼叫请求,你会得到较少的消息。如果消息队列中的数量是非常小的,你可能无法获得在特定ReceiveMessage响应的任何消息;在这种情况下,你应该重复的请求。

Due to the distributed nature of the queue, a weighted random set of machines is sampled on a ReceiveMessage call. That means only the messages on the sampled machines are returned. If the number of messages in the queue is small (less than 1000), it is likely you will get fewer messages than you requested per ReceiveMessage call. If the number of messages in the queue is extremely small, you might not receive any messages in a particular ReceiveMessage response; in which case you should repeat the request.

MaxNumberOfMessages 的:消息返回的最大数量。 SQS不返回更多的消息比这个值,但可能返回较少

MaxNumberOfMessages: Maximum number of messages to return. SQS never returns more messages than this value but might return fewer.

这篇关于检索SQS多封邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆