activemq-artemis/examples/jms/message-group2
Howard Gao 293b242ffc ACTIVEMQ6-43 Replace License Headers on codebase
upgrade mycila plugin to 2.6
2014-12-01 10:20:24 +08:00
..
src/main ACTIVEMQ6-43 Replace License Headers on codebase 2014-12-01 10:20:24 +08:00
pom.xml More name changes to activemq 2014-11-19 16:01:54 -05:00
readme.html More name changes to activemq 2014-11-19 16:01:54 -05:00

readme.html

<html>
  <head>
    <title>ActiveMQ 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 via a connection factory with ActiveMQ.</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 belongs to the group.</li>
     
     <p>You can make any message belong to a message group by setting a 'group-id' on the connection factory. All producers created via this connection factory will set that group id on its messages.
     In this example we set the group id 'Group-0'on a connection factory and send messages via 2 different producers and check that only 1 consumer receives them. </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>&lt;autogroup&gt;true&lt;/autogroup&gt;</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>
     <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 2 JMS message producers on the session. This will be used to send the messages.</li>
        <pre class="prettyprint">
          <code>
              MessageProducer producer1 = session.createProducer(queue);

              MessageProducer producer2 = session.createProducer(queue);</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 using each producer</li>
        <pre class="prettyprint">
           <code>
         int msgCount = 10;
         for (int i = 0; i < msgCount; i++)
         {
            TextMessage m = session.createTextMessage("producer1 message " + i);
            producer1.send(m);
            System.out.println("Sent message: " + m.getText());
            TextMessage m2 = session.createTextMessage("producer2 message " + i);
            producer2.send(m2);
            System.out.println("Sent message: " + m2.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("producer1 message " + 0);
            for (int i = 0; i < msgCount; i++)
            {
               String receiver = messageReceiverMap.get("producer1 message " + i);
               if (!trueReceiver.equals(receiver))
               {
                  System.out.println("Group message [producer1 message " + i + "] went to wrong receiver: " + receiver);
                  result = false;
               }
               receiver = messageReceiverMap.get("producer2 message " + i);
               if (!trueReceiver.equals(receiver))
               {
                  System.out.println("Group message [producer2 message " + i + "] 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-grouping2">Message Grouping chapter</a></li>
     </ul>
     
  </body>
</html>