JMS: modulus operator in message selector
I want to load balance JMS messages using message selectors.
A m开发者_开发问答essage has a property "EntitiyIX".
The selectors shall be like:
"EntitiyIX Modulus 2 == 0" ==> route to queue A
"EntitiyIX Modulus 2 != 0" ==> route to queue B
What's the operator to calculated the modulus in a JMS message selector?
Thanks, Alex
Assuming that the property is an integer, then I believe you could do
- (EntityIX / 2) = ((EntityIX+1) / 2) -> route to A
- (EntityIX / 2) != ((EntityIX+1) / 2) -> route to B
According to the API (http://download.oracle.com/javaee/1.4/api/javax/jms/Message.html - scroll down to "Message Selectors") there isn't a modulus operator.
What @Robin suggested sounds right.
In case you want to use more than two consumers try this:
Put a Content Enricher in front of the message consumer. Let the Content Enricher calculate a hash value that is something in the range from zero to one. Choose a simple and predictable hash function. For an order number you could divide the last two digits of the number by 100. Save that hash value at the message, let's say in the property X.
Then you would configure three message consumers with the following message selectors: "X < 1/3", "1/3 <= X and X < 2/3", "2/3 <= X".
If you are allowed to change the message sender, add the property before the message is sent. In this case the content enricher is redundant.
value=2
or any Integer
EntityIX-((EntityIX/value)*value)=0
-> route to A
EntityIX-((EntityIX/value)*value)<>0
-> route to B
精彩评论