简体   繁体   English

使用AWS SDK for PHP获取SQS队列中的所有消息

[英]Get all messages in SQS queue using AWS SDK for PHP

I am trying to retrieve all the messages in the queue using the AWS PHP SDK . 我正在尝试使用AWS PHP SDK检索队列中的所有消息。

Earlier there used to be a get_queue_size() method to get the queue size and then I would iterate through the loop to get all the messages. 之前曾经有过get_queue_size()方法来获取队列大小,然后我会遍历循环以获取所有消息。

In the newest SDK I don't see such a method. 在最新的SDK中,我没有看到这样的方法。 Link 链接

Can someone tell me how I can receive all the messages in the queue using the latest SDK for PHP ? 有人能告诉我如何使用最新的PHP SDK接收队列中的所有消息吗?

You can get all the messages in the queue, you just can't get them all at once. 您可以获取队列中的所有消息,但您无法一次性获取所有消息。 You request messages, and specify the max you want up to a max of 10 at a time , anymore than that, and you'll need to request another set of messages until your queue is empty (and even then you need to constantly poll SQS if there is a possibility that new messages will be coming in at anytime). 您请求消息,并且一次指定最多10个最大值 ,并且您需要请求另一组消息,直到您的队列为空(甚至那时您需要不断轮询SQS如果新消息有可能随时进入)。

Also important to remember that even if you have less than 10 messages in the queue, and you request the max of 10 (and even if there are no other clients currently polling), you still may not get all of the messages in the queue on a given call - you need to poll repeatedly. 同样重要的是要记住,即使队列中的消息少于10条,并且您请求最多10条消息(即使当前没有其他客户端轮询),您仍然可能无法获取队列中的所有消息给定的呼叫 - 您需要反复轮询。

To get the queue size, use the GetQueueAttributes operation : 要获取队列大小,请使用GetQueueAttributes操作

$result = $client->getQueueAttributes(array(
    'QueueUrl' => '{{QUEUE_URL}}',
    'AttributeNames' => array('ApproximateNumberOfMessages'),
));

$queueSize = $result['Attribute']['ApproximateNumberOfMessages'];

To get messages from the queue, use the ReceiveMessage operation . 要从队列中获取消息,请使用ReceiveMessage操作 The other answer by EJ Brennan is informative concerning that matter. EJ Brennan的另一个答案就此事提供了信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM