Chapter 15. Filtering Messages
AMQ Broker provides a powerful filter language based on a subset of the SQL 92 expression syntax. The filter language uses the same syntax as used for JMS selectors, but the predefined identifiers are different. The table below lists the identifiers that apply to a AMQ Broker message.
Identifier | Attribute |
---|---|
AMQPriority |
The priority of a message. Message priorities are integers with valid values from |
AMQExpiration | The expiration time of a message. The value is a long integer. |
AMQDurable |
Whether a message is durable or not. The value is a string. Valid values are |
AMQTimestamp | The timestamp of when the message was created. The value is a long integer. |
AMQSize |
The value of the |
Any other identifiers used in core filter expressions are assumed to be properties of the message. For documentation on selector syntax for JMS Messages, see the Java EE API.
15.1. Configuring a Queue to Use a Filter
You can add a filter to the queues you configure in BROKER_INSTANCE_DIR/etc/broker.xml
. Only messages that match the filter expression enter the queue.
Procedure
Add the
filter
element to the desiredqueue
and include the filter you want to apply as the value of the element. In the example below, the filterNEWS='technology'
is added to the queuetechnologyQueue
.<configuration> <core> ... <addresses> <address name="myQueue"> <anycast> <queue name="myQueue"> <filter string="NEWS='technology'"/> </queue> </anycast> </address> </addresses> </core> </configuration>
15.2. Filtering JMS Message Properties
The JMS specification states that a String property must not be converted to a numeric type when used in a selector. For example, if a message has the age
property set to the String value 21
, the selector age > 18
must not match it. This restriction limits STOMP clients because they can send only messages with String properties.
The JMS specification also states that hyphens cannot be used as property identifiers, making them unavailable for use in filters. However, this constraint can be overcome by using the hyphenated_props:
prefix.
Configuring a Filter to Convert a String to a Number
To convert String properties to a numeric type, add the prefix convert_string_expressions:
to the value of the filter
.
Procedure
Edit
BROKER_INSTANCE_DIR/etc/broker.xml
by applying the prefixconvert_string_expressions:
to the desiredfilter
. The example below edits thefilter
value fromage > 18
toconvert_string_expressions:age > 18
.<configuration> <core> ... <addresses> <address name="myQueue"> <anycast> <queue name="myQueue"> <filter string="convert_string_expressions='age > 18'"/> </queue> </anycast> </address> </addresses> </core> </configuration>
Enabling a Filter to Use Hyphens
To enable the use of hyphens when filtering JMS properties, add the prefix hyphenated_props:
to the value of the filter
.
Procedure
Edit
BROKER_INSTANCE_DIR/etc/broker.xml
by applying the prefixhyphenated_props:
to the desiredfilter
. In the example below, afilter
is edited so that it can select for the hyphenated propertyfoo-bar
.<configuration> <core> ... <addresses> <address name="myQueue"> <anycast> <queue name="myQueue"> <filter string="hyphenated_props='foo-bar = 0'"/> </queue> </anycast> </address> </addresses> </core> </configuration>