A JMS message selector is a boolean expression, written in a subset of SQL-92 conditional syntax, that a consumer attaches when it subscribes. The JMS provider evaluates it against each message's header fields and properties, and delivers only the messages for which it is true. This lets several consumers share one queue or topic and each receive only the messages they care about, without filtering in application code.
Selectors are part of the Java Message Service specification, now maintained as Jakarta Messaging (package jakarta.jms since Jakarta EE 9 and Spring Boot 3). Everything below applies to both the old javax.jms and the new jakarta.jms namespaces.
A selector is a string passed when a consumer or subscriber is created. It is evaluated against:
JMSPriority, JMSType, JMSCorrelationID, JMSMessageID, JMSDeliveryMode and JMSTimestamp.setStringProperty("type", "order").A selector can never look at the message body. If you need to filter on something inside a TextMessage or ObjectMessage, the producer must copy that value into a property first.
Supported syntax includes:
| Feature | Example |
|---|---|
| Comparison | amount >= 100, region <> 'EU' |
| Logical operators | AND, OR, NOT |
| Range | priority BETWEEN 4 AND 9 |
| Set membership | country IN ('IN', 'US', 'DE') |
| Pattern match | customer LIKE 'ACME%' |
| Null checks | coupon IS NULL, coupon IS NOT NULL |
| Arithmetic | price * quantity > 1000 |
String literals use single quotes. A property that does not exist evaluates to unknown, so a message without it does not match a comparison on it.
The behaviour differs slightly between the two messaging styles.
The selector is fixed for the life of the consumer. To change the filter you must close the consumer and create a new one.
The practical risk on queues is orphaned messages. If a message matches no active consumer's selector, it waits on the queue until its time to live expires or a matching consumer appears, and it can grow the backlog silently.
The producer decides which values are available for filtering. With Spring's JmsTemplate you set them through a MessagePostProcessor:
Calling publish("Order #42", "order", 10) and publish("Welcome email", "notification", 1) puts two messages on the same queue with different properties.
In Spring, the selector attribute of @JmsListener applies the filter:
The first listener receives only Order #42, the second only Welcome email. With the plain JMS 2.0 API the same filter is the second argument to createConsumer:
For durable topic subscriptions, pass the selector to createDurableConsumer or createSharedDurableConsumer.
Selectors are convenient, but they are not always the best tool.
| Approach | Best when | Trade-off |
|---|---|---|
| Message selector | A few consumers need different subsets of one destination | Provider evaluates every message; can be slow on large backlogs |
| Separate queues or topics | Message types are stable and clearly distinct | More destinations to manage, producer must route |
| Topic hierarchy or wildcard (broker specific) | Routing by a hierarchical key | Not part of the JMS standard |
| Filtering in the consumer | Filter needs the message body or external data | Messages are delivered, then discarded |
Brokers such as ActiveMQ Artemis and IBM MQ can evaluate selectors efficiently, but each message must still be inspected. On a very deep queue with many selective consumers, separate destinations usually scale better. Check your broker's documentation for how it indexes or optimises selectors.
No. Selectors only see header fields and properties. Copy the value you need into a property when you send the message.
On a queue they remain in the queue until a matching consumer takes them or they expire. On a topic they are dropped for that subscriber, and other subscribers are unaffected.
The provider (broker) evaluates it, so non-matching messages are not sent over the network to the consumer. Some providers also evaluate it client-side in specific configurations, so check your provider's behaviour.
No. A selector is set when the consumer is created. Close it and create a new consumer with the new expression.
JMS message selectors let a consumer receive only the messages whose headers and properties satisfy an SQL-92 style expression. They remove filtering code from consumers and reduce delivery of irrelevant messages, but they cannot read the body, they are fixed per consumer, and unmatched queue messages can pile up. Use them for modest, well-defined filtering and prefer separate destinations when volumes grow large.