您如何在确保每个实体的 FIFO 的同时并行处理消息? [英] How do you process messages in parallel while ensuring FIFO per entity?

查看:19
本文介绍了您如何在确保每个实体的 FIFO 的同时并行处理消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您的系统中有一个实体,例如Person",并且您想要处理修改各种 Person 实体的事件.重要的是:

Let's say you have an entity, say, "Person" in your system and you want to process events that modify various Person entities. It is important that:

  • 按 FIFO 顺序处理同一个人的事件
  • 多人事件流由不同的线程/进程并行处理

我们有一个使用共享数据库和锁来解决这个问题的实现.线程竞争获取Person 的锁,然后在获取锁后按顺序处理事件.我们想转移到消息队列以避免轮询和锁定,我们认为这会减少数据库的负载并简化消费者代码的实现.

We have an implementation that solves this using a shared database and locks. Threads compete to acquire the lock for a Person and then process events in order after acquiring the lock. We'd like to move to a message queue to avoid polling and locking, which we feel would reduce load on the DB and simplify the implementation of the consumer code.

我对 ActiveMQ、RabbitMQ 和 HornetQ 进行了一些研究,但我没有看到明显的实现方式.

I've done some research into ActiveMQ, RabbitMQ, and HornetQ but I don't see an obvious way to implement this.

ActiveMQ 支持消费者订阅通配符,但我没有看到将每个队列上的并发数限制为 1 的方法.如果我可以这样做,那么解决方案将很简单:

ActiveMQ supports consumer subscription wildcards, but I don't see a way to limit the concurrency on each queue to 1. If I could do that, then the solution would be straightforward:

  • 以某种方式告诉代理允许所有以/queue/person 开头的队列的并发为 1.
  • 发布者使用队列名称中的人员 ID 将事件写入队列.例如:/queue/person.20
  • 消费者使用通配符订阅队列:/queue/person.>
  • 每个消费者都会收到来自不同人员队列的消息.如果所有的人队列都在使用中,那么一些消费者可能会闲着,这没关系
  • 在处理完一条消息后,消费者发送一个 ACK​​,它告诉代理它已经完成了该消息的处理,并允许该 Person 队列的另一条消息被发送到另一个消费者(可能是同一个)

ActiveMQ 接近:您可以进行通配符订阅并启用独占消费者",但这种组合会导致单个消费者接收发送到所有匹配队列的所有消息,从而将所有人员的并发性降低到 1.我觉得我遗漏了一些明显的东西.

ActiveMQ came close: You can do wildcard subscriptions and enable "exclusive consumer", but that combination results in a single consumer receiving all messages sent to all matching queues, reducing your concurrency to 1 across all Persons. I feel like I'm missing something obvious.

问题:

  • 有没有办法通过任何主要的消息队列实现来实现上述方法?我们对选择持相当开放的态度.唯一的要求是它可以在 Linux 上运行.
  • 有没有其他方法可以解决我没有考虑的一般问题?

谢谢!

推荐答案

看起来 JMSXGroupID 正是我要找的.来自 ActiveMQ 文档:

It looks like JMSXGroupID is what I'm looking for. From the ActiveMQ docs:

http://activemq.apache.org/message-groups.html

他们的股票价格示例用例正是我所追求的.我唯一担心的是如果单个消费者死亡会发生什么.希望代理会检测到并选择另一个消费者与该组 ID 相关联.

Their example use case with stock prices is exactly what I'm after. My only concern is what happens if the single consumer dies. Hopefully the broker will detect that and pick another consumer to associate with that group id.

这篇关于您如何在确保每个实体的 FIFO 的同时并行处理消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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