如何使用java和spring 3.0从JMS主题(而不是队列)同时处理多个消息?

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

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.

我最后要做的是创建多个DefaultMessageListenerContainers,其中concurrency = 1,然后将逻辑放在消息监听器中,这样只有一个线程可以处理给定的消息ID.

解决方法:

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

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

我想也许你需要重新考虑你的设计,虽然我不确定我的建议.同时消费发布/订阅消息似乎是一件非常合理的事情,但是如何避免同时向所有消费者传递相同的消息?

上一篇:select()如何警告fd变为“准备好”?


下一篇:java – Jms消息仅在消息确认之前确认