gpt4 book ai didi

java - 如何使用 java 和 spring 3.0 同时处理来自 JMS 主题(不是队列)的多条消息?

转载 作者:IT老高 更新时间:2023-10-28 13:55:14 24 4
gpt4 key购买 nike

请注意,我希望多个消息监听器同时处理来自主题的连续消息。此外,我希望每个消息监听器都以事务方式操作,以便给定消息监听器中的处理失败会导致该监听器的消息保留在主题上。

spring DefaultMessageListenerContainer 似乎只支持 JMS 队列的并发。

我需要实例化多个 DefaultMessageListenerContainers 吗?

如果时间沿着纵轴流动:

ListenerA reads msg 1        ListenerB reads msg 2        ListenerC reads msg 3
ListenerA reads msg 4 ListenerB reads msg 5 ListenerC reads msg 6
ListenerA reads msg 7 ListenerB reads msg 8 ListenerC reads msg 9
ListenerA reads msg 10 ListenerB reads msg 11 ListenerC reads msg 12
...

更新:
感谢@T.Rob 和@skaffman 的反馈。

我最终做的是使用 concurrency=1 创建多个 DefaultMessageListenerContainers,然后将逻辑放入消息监听器中,以便只有一个线程可以处理给定的消息 ID。

最佳答案

您不想要多个 DefaultMessageListenerContainer 实例,不,但您需要将 DefaultMessageListenerContainer 配置为并发,使用 concurrentConsumers property :

Specify the number of concurrent consumers to create. Default is 1.

Specifying a higher value for this setting will increase the standard level of scheduled concurrent consumers at runtime: This is effectively the minimum number of concurrent consumers which will be scheduled at any given time. This is a static setting; for dynamic scaling, consider specifying the "maxConcurrentConsumers" setting instead.

Raising the number of concurrent consumers is recommendable in order to scale the consumption of messages coming in from a queue. However, note that any ordering guarantees are lost once multiple consumers are registered. In general, stick with 1 consumer for low-volume queues.

但是,底部有一个很大的警告:

Do not raise the number of concurrent consumers for a topic. This would lead to concurrent consumption of the same message, which is hardly ever desirable.

这很有趣,并且在您考虑时很有意义。如果您有多个 DefaultMessageListenerContainer 实例,也会发生同样的情况。

我认为也许你需要重新考虑你的设计,虽然我不确定我会建议什么。 pub/sub 消息的并发消费似乎是一件非常合理的事情,但是如何避免将相同的消息同时传递给所有消费者呢?

关于java - 如何使用 java 和 spring 3.0 同时处理来自 JMS 主题(不是队列)的多条消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3088814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com