<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <html> <head> <title>ActiveMQ Artemis Message Group Example</title> <link rel="stylesheet" type="text/css" href="../../../common/common.css" /> <link rel="stylesheet" type="text/css" href="../../../common/prettify.css" /> <script type="text/javascript" src="../../../common/prettify.js"></script> </head> <body onload="prettyPrint()"> <h1>Message Group Example</h1> <pre>To run the example, simply type <b>mvn verify</b> from this directory, <br>or <b>mvn -PnoServer verify</b> if you want to start and create the server manually.</pre> <p>This example shows you how to configure and use message groups with ActiveMQ Artemis.</p> <p>Message groups are sets of messages that has the following characteristics: </p> <li>Messages in a message group share the same group id, i.e. they have same JMSXGroupID string property values.</li> <li>Messages in a message group will be all delivered to no more than one of the queue's consumers. The consumer that receives the first message of a group will receive all the messages that belong to the group.</li> <p>You can make any message belong to a message group by setting its 'JMXGroupID' string property to the group id. In this example we create a message group 'Group-0'. And make such a message group of 10 messages. It also create two consumers on the queue where the 10 'Group-0' group messages are to be sent. You can see that with message grouping enabled, all the 10 messages will be received by the first consumer. The second consumer will receive none. </p> <p>Alternatively, ActiveMQ's connection factories can be configured to <em>auto group</em> messages. By setting <code>autogroup</code> to </code>true</code> on the <code>ActiveMQConnectionFactory</code> (or setting <code><autogroup>true</autogroup></code> in <code>activemq-jms.xml</code>'s connection factory settings), a random unique id will be picked to create a message group. <em>Every messages</em> sent by a producer created from this connection factory will automatically be part of this message group.</p> <h2>Example step-by-step</h2> <ol> <li>First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the <code>client-jndi.properties</code> file in the directory <code>../common/config</code></li> <pre class="prettyprint"> <code>InitialContext initialContext = getContext();</code> </pre> <li>We look-up the JMS queue object from JNDI</li> <pre class="prettyprint"> <code>Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");</code> </pre> <li>We look-up the JMS connection factory object from JNDI</li> <pre class="prettyprint"> <code>ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");</code> </pre> <li>We create a JMS connection</li> <pre class="prettyprint"> <code>connection = cf.createConnection();</code> </pre> <li>We create a JMS session. The session is created as non transacted and will auto acknowledge messages.</li> <pre class="prettyprint"> <code>Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code> </pre> <li>We create a JMS message producer on the session. This will be used to send the messages.</li> <pre class="prettyprint"> <code>MessageProducer messageProducer = session.createProducer(topic);</code> </pre> <li>We create two consumers.</li> <pre class="prettyprint"> <code> MessageConsumer consumer1 = session.createConsumer(queue); consumer1.setMessageListener(new SimpleMessageListener("consumer-1")); MessageConsumer consumer2 = session.createConsumer(queue); consumer2.setMessageListener(new SimpleMessageListener("consumer-2")); </code> </pre> <li>We create and send 10 text messages with group id 'Group-0'</li> <pre class="prettyprint"> <code> int msgCount = 10; TextMessage[] groupMessages = new TextMessage[msgCount]; for (int i = 0; i < msgCount; i++) { groupMessages[i] = session.createTextMessage("Group-0 message " + i); groupMessages[i].setStringProperty("JMSXGroupID", "Group-0"); producer.send(groupMessages[i]); System.out.println("Sent message: " + groupMessages[i].getText()); } </code> </pre> <li>We start the connection.</li> <pre class="prettyprint"> <code>connection.start();</code> </pre> <li>We check the group messages are received by only one consumer</li> <pre class="prettyprint"> <code> String trueReceiver = messageReceiverMap.get(groupMessages[0].getText()); for (TextMessage grpMsg : groupMessages) { String receiver = messageReceiverMap.get(grpMsg.getText()); if (!trueReceiver.equals(receiver)) { System.out.println("Group message [" + grpMsg.getText() + "[ went to wrong receiver: " + receiver); result = false; } } </code> </pre> <li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li> <pre class="prettyprint"> <code>finally { if (initialContext != null) { initialContext.close(); } if (connection != null) { connection.close(); } }</code> </pre> </ol> <h2>More information</h2> <ul> <li>User Manual's <a href="../../../docs/user-manual/en/html_single/index.html#message-grouping">Message Grouping chapter</a></li> </ul> </body> </html>