-->

Filtering in JMS based on body content

2019-09-14 10:17发布

问题:

I m implementing Pub/Sub model using JMS. I send a message from Pub to all Subscribers. I want that subscribers should get filtered messages based on some string in actual message body.

For example a subscriber subscribe to a topic 'sports' and should receive only those posts which has keyword 'cricket' in it in the message text body.

p.s. I dont want to use message selectors.

How can I implement this.

Thanks and Regards.

回答1:

Take a look at apache camel. It provides a means of routing and filtering messages and has excellent integration with Active MQ.



回答2:

You can use no mechanism for filtering messages on a topic based on the Message Body contents. Usually a JMS Selector is used for filtering messages but even this does not work for Body Contents:

From The Java EE 6 Tutorial:

A message selector cannot select messages on the basis of the content of the message body..

The issue here is that you have to first receive (that is consume) the message and then extract its contents which precludes the case of any kind of Body filtering.



回答3:

You cannot do that with JMS itself.

What you typically do is to make the sending application use different queues depending on message type (orders, customer prospects, invoices, status reports or whatnot). If you don't want to use separate queues, you can at least make the sending application mark the message with some property that you can filter on using a selector.

In some cases, where you still need to do routing and/or filtering based on the actual content of a message, there are tailor made software for that kind of thing. Apache Camel and Mule ESB are two options.