简体   繁体   English

Spring中与JMS主题的并发

[英]Concurrency with JMS topic in Spring

How to use concurrency in jms topics using spring? 如何使用Spring在JMS主题中使用并发? Like for queues we can set maxConcurrentConsumers properties to the desired number. 像队列一样,我们可以将maxConcurrentConsumers属性设置为所需的数字。 But if I do that for a JMS topic, it leads to concurrent consumption of the same message, which is not desirable. 但是,如果我针对JMS主题这样做,则会导致并发使用同一条消息,这是不希望的。

Can somebody please point me in the right direction? 有人能指出我正确的方向吗?

You are using the DefaultMessageListenerContainer in a wrong way. 您以错误的方式使用DefaultMessageListenerContainer From the docs: 从文档:

Note that dynamic scaling only really makes sense for a queue in the first place; 请注意,动态缩放首先只对队列有意义。 for a topic, you will typically stick with the default number of 1 consumer, else you'd receive the same message multiple times on the same node. 对于一个主题,您通常会坚持使用1个使用者的默认数量,否则您将在同一节点上多次收到同一条消息。

To help your situation you may forward messages from the topic to a queue and let your consumers consume from this queue. 为了帮助您解决问题,您可以将主题中的消息转发到队列,并让您​​的使用者从该队列中消费。 If your JMS provider has some forwarding feature, than this is merely a configuration change to your application. 如果您的JMS提供程序具有某些转发功能,那么这仅仅是对应用程序的配置更改。

Well you said it yourself in a comment, but just to elaborate. 好吧,您自己在评论中说了这一点,但只是为了详细说明。

Inside your listener, you can still create multiple threads and execute the processing in an asynchronous way . 在侦听器内部,您仍然可以创建多个线程并以异步方式执行处理。 As soon as you get your messages dispatch it to a asynchronous consumer. 一旦收到消息,便将其分发给异步使用者。 This is fairly easily implemented through some sort of executor for example. 例如,通过某种执行程序可以很容易地实现这一点。

I would really go for the guava's EventBus . 我真的会去买番石榴的EventBus It is so simple, yet so easy to set up and use. 它是如此简单,却易于设置和使用。 But this is your choice really. 但这确实是您的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM