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

查看:17
本文介绍了如何在确保每个实体 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:

  • 以先进先出的顺序处理同一个人的事件
  • 多个 Person 事件流由不同的线程/进程并行处理

我们有一个使用共享数据库和锁来解决这个问题的实现.线程竞争获取一个 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天全站免登陆