消息传递模式问题
进程 A 正在计算对象 a1、a2、a3 等的值,并将结果发送到中间件队列 (RabbitMQ)。消费者读取队列并进一步处理这些结果。 进程 A 必须定期发送这些值的快照,以便消费者可以进行一些其他计算。这些对象的值可能会独立更改。队列可能看起来像这样 a1, a1, a2, a1, a2, a2, a3... 消费者处理队列中的每个项目。快照必须包含所有对象,消费者将一次性处理所有对象的此消息。
所以要求是有一个像这样的队列: a1, a1, a3, a2, a2, [snapshot, a1, a2, a3], a3, a1 ... 问题是这些项目有不同的类型:一种类型对于 a1、a2 等对象以及其他快照。这意味着它们应该在不同的队列中处理,但在这种情况下存在竞争条件:消费者可能会在处理快照之前处理对象。
有什么模式可以解决这个(很常见)问题吗?我们使用 RabbitMQ 进行消息队列。
Process A is calculating values for objects a1, a2, a3 etc. and is sending results to the middleware queue (RabbitMQ). Consumers read the queue and process these results further.
Periodically process A has to send a snapshot of these values, so consumers could do some other calculations. Values for these objects might change independently. The queue might look like this a1, a1, a2, a1, a2, a2, a3... Consumers process each item in the queue. The snapshot has to contain all objects and consumers will process this message for all objects in one go.
So the requirement is to have a queue like this: a1, a1, a3, a2, a2, [snapshot, a1, a2, a3], a3, a1 ... The problem is that these items are of different types: one type for objects like a1, a2 and other for a snapshot. This means that they should be processed in a diferent queues, but in that case there is a race condition: consumers might process objects before processing a snapshot.
Is there any pattern to solve this (quite common) problem? We are using RabbitMQ for message queueing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每个消费者使用一个队列,并使用内容类型前缀来指示正在传递的消息的类型。我认为AMQP消息中可能有一个“type”属性,但我从未使用过它,所以我不知道它是否适用于这个问题。
Use a single queue per consumer and a content-type prefix to indicate the type of message being delivered. In think there may be a 'type' attribute in AMQP messages, but I've never used it, so I don't know if it's applicable to this problem.