java JMS - 消息选择器如何处理多个队列和主题消费者?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2828653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JMS - How do message selectors work with multiple queue and topic consumers?
提问by Stephen Harmon
Say you have a JMS queue, and multiple consumers are watching the queue for messages. You want one of the consumers to get all of a particular type of message, so you decide to employ message selectors.
假设您有一个 JMS 队列,并且多个消费者正在监视队列以获取消息。您希望其中一个消费者获得所有特定类型的消息,因此您决定使用消息选择器。
For example, you define a property to go in your JMS message header named, targetConsumer. Your message selector, which you apply to the consumer known as, A, is something like WHERE targetConsumer = 'CONSUMER_A'.
例如,您定义了一个属性以放入名为targetConsumer. 您的消息选择器,您将其应用于称为 的消费者A,类似于WHERE targetConsumer = 'CONSUMER_A'。
It's clear that consumer A will now just grab messages with the property set like it is in in the example. Will the other consumers have awareness of that, though? IOW, will another consumer, unconstrained by a message selector, grab the CONSUMER_Amessages, if it looks at the queue before Consumer A? Do I need to apply message selectors like, WHERE targetConsumer <> 'CONSUMER_A'to the others?
很明显,消费者 A 现在将像示例中一样获取具有属性集的消息。不过,其他消费者会意识到这一点吗?IOW,如果另一个消费者不受消息选择器的约束,它会抓取CONSUMER_A消息,如果它在 Consumer 之前查看队列A吗?我是否需要像WHERE targetConsumer <> 'CONSUMER_A'其他人一样应用消息选择器?
I am RTFMing and gathering empirical data now, but was hoping someone might know off the top of their head.
我现在正在 RTFMing 和收集经验数据,但希望有人可能知道他们的头脑。
采纳答案by Vineet Reynolds
When multiple consumers use the same queue, message selectors need to configured correctly across these consumers so that there is no conflict in determining the intended consumer.
当多个消费者使用同一个队列时,需要在这些消费者之间正确配置消息选择器,以便在确定预期消费者时不会发生冲突。
In the case of message-driven-beans (a consumer of JMS messages), the selector can be specified in the ejb-jar.xml file thereby allowing for the configuration to be done at deployment time (instead of the opposing view of specifying the message selector during development).
在消息驱动 bean(JMS 消息的使用者)的情况下,可以在 ejb-jar.xml 文件中指定选择器,从而允许在部署时完成配置(而不是指定开发期间的消息选择器)。
Edit: In real life, this would make sense when different consumers are responsible for processing messages containing the same headers (often generated by the same producer) written onto the same queue. For instance, message selectors could be used in a trading application, to differentiate between buy and sell orders, when the producer is incapable of writing the JMS messages onto two separate buy and sell queues.
编辑:在现实生活中,当不同的消费者负责处理包含写入同一队列的相同标头(通常由同一生产者生成)的消息时,这将是有意义的。例如,当生产者无法将 JMS 消息写入两个独立的买入和卖出队列时,可以在交易应用程序中使用消息选择器来区分买入和卖出订单。
回答by Shashi
Yes, another consumer which is not using any message selector will get message intended for consumer A (or for that matter any message on top of the queue). Hence when sharing a queue, consumer applications must be disciplined and pick only those messages intended for them.
是的,另一个不使用任何消息选择器的消费者将获得用于消费者 A 的消息(或就此而言队列顶部的任何消息)。因此,在共享队列时,消费者应用程序必须受到纪律处分,并且只选择那些为他们准备的消息。
回答by Axel Podehl
The 'first' JMS message consumer from a queue will pick up the message if the selector matches. What 'first' means is an implementation detail (could be round-robin, based on priority or network closeness). So when using selectors on queues you need to make sure that these selectors are 'non overlapping'.
如果选择器匹配,则队列中的“第一个”JMS 消息使用者将获取该消息。“第一”的意思是一个实现细节(可以是循环的,基于优先级或网络接近度)。因此,在队列上使用选择器时,您需要确保这些选择器“不重叠”。
More formally: no message must exist that matches 2 selectors on the same queue
更正式地说:必须不存在与同一队列中的 2 个选择器匹配的消息
This is yet another disadvantage of queues versus topics - in practice you should always consider using topics first. With a topic each matching consumer receives the message.
这是队列与主题的另一个缺点 - 在实践中,您应该始终首先考虑使用主题。通过主题,每个匹配的消费者都会收到消息。

