How do I get my MDB to ignore messages coming from itself?

I have a few clients that are both consumers and subscribers to a single topic on an ActiveMQ message broker. All the clients share the same code, they are in fact using exactly the same Enterprise Application consisting of: (1) an EJB producing a message, and (2) an MDB consuming the message.

The problem is basically that if I have clients A, B and C, and if A sends out a message, then A, B, C will all receive the message. I don't want A to receive its own message.

So I played around with various solutions, the best one I came up with was to set a string property on the outgoing message, e.g. source=myVeryOwnID. Then in the MDB, I set up a message selector like source <> 'myVeryOwnID'.

Unfortunately, that is a poor solution because I would have to set this ID in the source code (in my case, within annotations). This means that when deploying a new client, I cannot simply give the .EAR file to someone, instead I have to specifically re-compile with a unique "source" property.

Ideally, I would like to use the MAC address as the ID, or perhaps an ID set within Glassfish (I am using GFv3).

Any solutions or ideas would be highly appreciated!

Upvotes: 3

Views: 945

Answers (3)

bsnyder
bsnyder

Reputation: 1199

ActiveMQ contains a method argument to address exactly this situation. Both the ActiveMQSession.createConsumer() method and the ActiveMQSession. createDurableSubscriber() method provide a variant that accepts an argument named noLocal. Set the noLocal argument to true to avoid receiving messages that were published locally on the same connection.

Bruce

Upvotes: 1

Henryk Konsek
Henryk Konsek

Reputation: 9168

What about the plain old System.GetProperty() and -D option? You can set the unique application ID as the system property:

-Dmyapp.id=A

For example in Tomcat you can pass system property via the JAVA_OPTS variable:

export JAVA_OPTS='-Dmyapp.id=A'

Then you can read it in the application:

String appId = System.getProperty("myapp.id")

All you have to do it to set system variable for each of your application server.

Upvotes: 0

Pascal Thivent
Pascal Thivent

Reputation: 570595

Using a "source" message attribute in the message and a message selector is IMHO the way to go. Now, if you don't want to hard code this in the MDB (in annotations), then use a deployment descriptor and set the message selector at packaging time.

Upvotes: 2

Related Questions