Message GroupsMessage 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 CaseLets 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 WorkWhen 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 <destinationPolicy> <policyMap> <policyEntries> <policyEntry queue=">" maxPageSize="1000"/> </policyEntries> </policyMap> </destinationPolicy> Using Message GroupsYou just need to change your JMS producers to fill in the Example: Mesasge message = session.createTextMessage("<foo>hey</foo>"); message.setStringProperty("JMSXGroupID", "IBM_NASDAQ_20/4/05"); ... producer.send(message); Closing a Message GroupYou 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. Example: Mesasge message = session.createTextMessage("<foo>hey</foo>"); message.setStringProperty("JMSXGroupID", "IBM_NASDAQ_20/4/05"); message.setIntProperty("JMSXGroupSeq", -1); ... producer.send(message); 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. ImplicationsMessage 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 GroupsActiveMQ support a boolean header called If the JMS connection is using Example: String groupId = message.getStringProperty("JMSXGroupId"); if (message.getBooleanProperty("JMSXGroupFirstForConsumer")) { // flush cache for groupId } To flush caches to ensure consistent state when faced with network errors. Adding New ConsumersIf 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 <destinationPolicy> <policyMap> <policyEntries> <policyEntry queue=">" timeBeforeDispatchStarts="200"/> </policyEntries> </policyMap> </destinationPolicy> The following code snippet shows how to wait for two consumers (or two seconds) before dispatch starts: <destinationPolicy> <policyMap> <policyEntries> <policyEntry queue=">" consumersBeforeDispatchStarts="2" timeBeforeDispatchStarts="2000"/> </policyEntries> </policyMap> </destinationPolicy> 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 ] See |