Message Groups are an enhancement to the Exclusive Consumer feature. They provide:
So logically Message Groups are like a parallel Exclusive Consumer. Rather than all messages going to a single consumer, the standard JMS header
Another way of explaining Message Groups is that it provides sticky load balancing of messages across consumers; where the
Example Use Case
Lets say we are doing some kind of order matching system where people are buying and selling things (stocks, shares, placing online bets, whatever). You want to have consumers who match bids and offers for different items (stocks / bets) so they want to keep in RAM for performance a sub-set of the data set. Therefore set the
How Message Groups Work
When a message is being dispatched to a consumer, the
If no consumer is associated with a message group a consumer is chosen. Said JMS
Note: as with message selector matching, grouping based on
Using Message Groups
You just need to change your JMS producers to fill in the
Closing a Message Group
You generally don't need to close a message group; just keep using it. However if you really do want to close a group you can add a negative sequence number.
This then closes the message group so if another message is sent in the future with the same message group ID it will be reassigned to a new consumer.
Message Groups mean you get the power of grid processing of messages across a cluster of consumers with reliability, auto-failover, load balancing but you can also order the processing of messages too. So its the best of both worlds. However using the above example, what Message Groups actually do is to partition your work load across consumers using a user definable partition strategy - the
The neat thing about this is that you can do neat things like use lots of RAM caching; keep the order for
The great thing is - to the application developer, it looks like a simple 1 consumer world where you process messages and do your job; leaving the broker to do all the hard stuff for you
In summary; if ordering or per-message caching and synchronization are in any way important to you then we highly recommend you use message groups to partition your traffic.
Getting Notified of Ownership Changes of Message Groups
ActiveMQ support a boolean header called
If the JMS connection is using
To flush caches to ensure consistent state when faced with network errors.
Adding New Consumers
If you have existing messages in the broker and add consumers at a later stage, it is a good idea to delay message dispatch start until all consumers are present (or at least to give enough time for them to subscribe). If you don't do that the first consumer will probably acquire all message groups and all messages will be dispatched to it. You can achieve this by using
When both consumersBeforeDispatchStarts and timeBeforeDispatchStarts are set to a value greater than zero, the dispatching will start as soon as the required number of consumers are present or the timeBeforeDispatchStarts timeout expires. If only consumersBeforeDispatchStarts is set then the timeout for consumers to connect is 1 second. If all consumers disconnect then message dispatch delay will be applied again at the next consumer connection.
Here's the example of the destination policy that delays dispatch for
The following code snippet shows how to wait for two consumers (or two seconds) before dispatch starts:
As the appropriate test case shows, adding a small time pause before dispatching or setting a minimum consumer number, ensures equal message group distribution.
Competing demands of memory consumption, load balancing, complexity, etc.
The default behavior which is limited to 1024 message groups in an LRU cache may not match you expectation w.r.t message order... some detail to explain:
MessageGroupHashBucket and SimpleMessageGroupMap message groups work by associating each group with a consumer.
SimpleMessageGroupMap keeps track of every group but suffers from unbounded memory use.
MessageGroupHashBucked keeps track of every group and has bounded memory use.
CachedMessageGroupMap has bounded memory use, but only keeps track of up to 1024 (or the maximum configured size) groups, then loses track of any groups older than the newest 1024.
In this way, if there are more groups than the maximum, ordering will be lost for the oldest groups.
Typically users would close groups such that the in memory set can be retained below the configured limits. Some usefull discussion at [ AMQ-6851 ]