How to make my MDB ignore messages coming from itself?

I have several clients who are both consumers and subscribers on the same topic in the ActiveMQ message broker. All clients have the same code, they actually use exactly the same Enterprise Application, consisting of: (1) the EJB that creates the message, and (2) the MDB that consumes the message.

The problem is that if I have clients A, B and C, and if A sends a message, then A, B, C everyone will receive the message. I do not want A to receive his own message.

So, I played with various solutions, the best of which I came up with is to set the line property in the outgoing message, for example. Source = myVeryOwnID . Then in MDB, I set up a message selector, for example source <> 'myVeryOwnID' .

Unfortunately, this is a bad decision, because I will need to set this identifier in the source code (in my case, in the annotations). This means that when deploying a new client, I can’t just transfer the .EAR file to someone, instead I have to specially recompile it with the unique “source” property.

Ideally, I would like to use the MAC address as an identifier, or perhaps the identifier set in Glassfish (I use GFv3).

Any solutions or ideas will be highly appreciated!

+3
source share
3 answers

Using the message attribute "source" in the message and the message selector is IMHO, the path. Now, if you do not want to hard code this in MDB (in annotations), use the deployment descriptor and set the message selector during packaging.

+2
source

ActiveMQ contains a method argument to solve this particular situation. Both the method ActiveMQSession.createConsumer()and the method ActiveMQSession. createDurableSubscriber()provide an option that takes an argument with a name noLocal. Set the argument noLocalto true to not receive messages posted locally on the same connection.

Bruce

+1

What about the simple old System.GetProperty () and -D option? You can set the unique identifier of the application as a system property:

-Dmyapp.id=A

For example, in Tomcat, you can pass a system property through 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 is set a system variable for each of your application server.

0
source

Source: https://habr.com/ru/post/1733750/


All Articles