JMS - How do message selectors work with multiple queue and topic consumers?
Asked Answered
S

3

9

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.

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'.

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_A messages, if it looks at the queue before Consumer A? Do I need to apply message selectors like, WHERE targetConsumer <> 'CONSUMER_A' to the others?

I am RTFMing and gathering empirical data now, but was hoping someone might know off the top of their head.

Steffi answered 13/5, 2010 at 16:51 Comment(0)
G
7

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).

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.

Gallows answered 13/5, 2010 at 17:15 Comment(1)
I don't see how this is answering the question. Do you mean to say that in the above described scenario an unconstrained message consumer will grab CONSUMER_A messages?Welter
S
0

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.

Soever answered 13/6, 2017 at 0:3 Comment(0)
H
0

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'.

More formally: no message must exist that matches 2 selectors on the same queue

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.

Hosfmann answered 13/6, 2017 at 7:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.