从 SQS 检索多条消息 [英] Retrieve multiple messages from SQS

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

问题描述

我在 SQS 中有多个消息.下面的代码总是只返回一个,即使有几十个可见(不在飞行中).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
 }

我也试过使用 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

The above always is run prior and gives me an int that is >1

感谢您的意见

推荐答案

AWS API 参考指南:查询/查询接收消息

由于队列的分布式特性,在 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天全站免登陆