如何使用 java 和 spring 3.0 同时处理来自 JMS 主题(不是队列)的多条消息? [英] How can I handle multiple messages concurrently from a JMS topic (not queue) with java and spring 3.0?

查看:13
本文介绍了如何使用 java 和 spring 3.0 同时处理来自 JMS 主题(不是队列)的多条消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Note that I'd like multiple message listeners to handle successive messages from the topic concurrently. In addition I'd like each message listener to operate transactionally so that a processing failure in a given message listener would result in that listener's message remaining on the topic.

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

The spring DefaultMessageListenerContainer seems to support concurrency for JMS queues only.

我需要实例化多个 DefaultMessageListenerContainers 吗?

Do I need to instantiate multiple 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.

What I ended up doing is creating multiple DefaultMessageListenerContainers with concurrency=1 and then putting logic in the message listener so that only one thread would process a given message id.

推荐答案

你不想要多个 DefaultMessageListenerContainer 实例,不,但你需要配置 DefaultMessageListenerContainer并发,使用 concurrentConsumers 属性:

You don't want multiple DefaultMessageListenerContainer instances, no, but you do need to configure the DefaultMessageListenerContainer to be concurrent, using the concurrentConsumers property:

指定并发数消费者去创造.默认为 1.

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

为此指定更高的值设置将提高标准预定并发级别运行时的消费者:这是有效的最少数量并发消费者将是安排在任何给定的时间.这是一个静态设置;对于动态缩放,考虑指定maxConcurrentConsumers"设置相反.

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.

提高并发数消费者是值得推荐的,以便扩展消息的消耗从队列中进来.但是,请注意失去任何订购保证一旦有多个消费者挂号的.一般来说,坚持 1低容量队列的消费者.

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.

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

However, there's big warning at the bottom:

不要增加一个主题的并发消费者数量.这会导致并发消费相同的消息,其中几乎是不可取的.

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 实例,也会发生同样的情况.

This is interesting, and makes sense when you think about it. The same would occur if you had multiple DefaultMessageListenerContainer instances.

我认为您可能需要重新考虑您的设计,尽管我不确定我的建议是什么.发布/订阅消息的并发消费似乎是一件非常合理的事情,但如何避免将相同的消息同时传递给所有消费者?

I think perhaps you need to rethink your design, although I'm not sure what I'd suggest. Concurrent consumption of pub/sub messages seems like a perfectly reasonable thing to do, but how to avoid getting the same message delivered to all of your consumers at the same time?

这篇关于如何使用 java 和 spring 3.0 同时处理来自 JMS 主题(不是队列)的多条消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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