| <div class="wiki-content maincontent"><h2>JMS Selectors</h2> |
| |
| <p>Selectors are a way of attaching a filter to a subscription to perform content based routing. Selectors are defined using SQL 92 syntax and typically apply to message headers; whether the standard properties available on a JMS message or custom headers you can add via the JMS code.</p> |
| |
| <p>Here is an example</p> |
| |
| <structured-macro ac:macro-id="f3768a53-c6dc-48df-b415-351bc7ffc0ba" ac:name="code" ac:schema-version="1"><plain-text-body> |
| JMSType = 'car' AND color = 'blue' AND weight > 2500 |
| </plain-text-body></structured-macro> |
| |
| <p>For more documentation on the detail of selectors see the reference on <a shape="rect" href="http://java.sun.com/j2ee/1.4/docs/api/javax/jms/Message.html">javax.jmx.Message</a>. <br clear="none"> |
| ActiveMQ supports some JMS defined properties, as well as some ActiveMQ ones - see <link><page ri:content-title="ActiveMQ Message Properties"></page><link-body>message properies </link-body></link> - that the selector can use.</p> |
| |
| <h3>Using XPath to filter messages</h3> |
| |
| <p>Apache ActiveMQ also supports XPath based selectors when working with messages containing XML bodies. To use an XPath selector use the following syntax</p> |
| |
| <structured-macro ac:macro-id="518e2e91-77e2-4043-8eb8-a0cb12d232f8" ac:name="code" ac:schema-version="1"><plain-text-body> |
| XPATH '//title[@lang=''eng'']' |
| </plain-text-body></structured-macro> |
| |
| <structured-macro ac:macro-id="a2264760-f437-4e30-aef0-a443c1339472" ac:name="tip" ac:schema-version="1"><parameter ac:name="title">Xalan dependency</parameter><rich-text-body> |
| <p>XPath support requires <a shape="rect" href="http://xalan.apache.org/index.html">Xalan</a> which is not part of the distribution. The Xalan jars needs to be manually added to lib directory or pulled into your app via maven </p> |
| <structured-macro ac:macro-id="50c8144b-97b0-4de2-866f-df74715a9173" ac:name="code" ac:schema-version="1"><plain-text-body><dependency> |
| <groupId>xalan</groupId> |
| <artifactId>xalan</artifactId> |
| <version>2.6.0</version> |
| </dependency></plain-text-body></structured-macro></rich-text-body></structured-macro> |
| |
| <h3>String Property Conversions / Selecting Against STOMP messages</h3> |
| |
| <p>The JMS spec states that a String property should not get converted to a numeric when used in a selector. So for example, if a message has the 'age' property set to String '21' then the following selector should not match it: 'age > 18'. Since ActiveMQ support STOMP client which can only send messages with string properties, that restriction is a bit limiting. If you want your JMS selectors to auto-convert String properties the the appropriate number type, just prefix the the selector with 'convert_string_expressions:'. If you changed selector in the previous example to be 'convert_string_expressions:age > 18', then it would match the message.</p> |
| </div> |
| |