activemq-artemis/examples/jms/message-group
Andy Taylor 8ecd255f98 ACTIVEMQ6-1 - Initial HornetQ Donation Commit
https://issues.apache.org/jira/browse/ACTIVEMQ6-1

This is the initial donation of the HornetQ codebase as per document http://incubator.apache.org/ip-clearance/hornetq.html
2014-11-10 10:31:25 -06:00
..
src/main ACTIVEMQ6-1 - Initial HornetQ Donation Commit 2014-11-10 10:31:25 -06:00
pom.xml ACTIVEMQ6-1 - Initial HornetQ Donation Commit 2014-11-10 10:31:25 -06:00
readme.html ACTIVEMQ6-1 - Initial HornetQ Donation Commit 2014-11-10 10:31:25 -06:00

readme.html

<html>
  <head>
    <title>HornetQ 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>

     <p>This example shows you how to configure and use message groups with HornetQ.</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, HornetQ's connection factories can be configured to <em>auto group</em> messages. By setting <code>autogroup</code> to </code>true</code> on the <code>HornetQConnectionFactory</code>
        (or setting <code>&lt;autogroup&gt;true&lt;/autogroup&gt;</code> in <code>hornetq-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>
     <p><i>To run the example, simply type <code>mvn verify</code> from this directory</i></p>

     <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 &lt; 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>