0

I was wondering if there was a best practice for notifying the end of an sqs queue. I am spawning a bunch of generic workers to consume data from a queue and I want to notify them that they can stop processing once they detect no more messages in the queue. Does sqs provide this type of feature?

4

2 回答 2

0

您的意思是“生产者有没有办法通知消费者它已完成发送消息?” . 如果是这样,那么不,没有。如果消费者调用“ReceiveMessage”并没有得到任何回复,或者“ApproximateNumberOfMessages”返回零,这并不能保证不再发送消息,甚至没有消息在传输中。并且生产者不能发送任何类型的“流结束”消息,因为只有一个消费者会收到它,而且它可能会乱序到达。即使您使用单独的通知机制(例如 SNS 主题)来通知所有消费者,也不能保证在所有消息传递之前不会到达 SNS 通知。

但是,如果您只是希望您的工作人员池在队列中没有消息时退出,那么请考虑将队列上的“ReceiveMessageWaitTimeSeconds”属性设置为其最大值 20 秒。当没有更多消息要处理时,ReceiveMessage 调用将阻塞长达 20 秒,以查看消息是否到达,而不是立即返回。

如果您担心释放资源,您可以让管理线程池查询 ApproximateNumberOfMessages 的任何东西定期扩展/缩小您的线程池。如果这样做,请注意返回的数字是 Approximate,即使 ApproximateNumberOfMessages 返回零,您也应该始终假设队列中可能还有一条或多条消息。

于 2013-05-18T08:44:24.643 回答
0

通过查看 SQS 的 right_aws ruby​​ gem 源代码,我发现队列中有 ApproximateNumberOfMessages 属性。您可以使用标准 API 调用请求。

您可以在此处找到更多信息,包括示例:

http://docs.amazonwebservices.com/AWSSimpleQueueService/latest/APIReference/Query_QueryGetQueueAttributes.html

有关如何使用 ruby​​ 中的 right_aws gem 执行此操作的更多信息,请查看:

于 2012-12-10T21:58:36.957 回答