activemq-artemis/examples/jms/topic-selector-example1
Clebert Suconic 3c44e26854 ACTIVEMQ6-3 renaming directories from activemq6 to activemq
https://issues.apache.org/jira/browse/ACTIVEMQ6-3

We are renaming packages from activemq6 to activemq as that's more generic and version independent
On this first commit I'm just renaming the directories otherwise the history would be lost. The next commit will rename the text on the directories.
If I squash these two commits git will make us delete / add again.
2014-11-17 09:33:36 -05:00
..
src/main ACTIVEMQ6-3 renaming directories from activemq6 to activemq 2014-11-17 09:33:36 -05:00
pom.xml Use new package names in config and classes 2014-11-11 18:28:18 +00:00
readme.html ACTIVEMQ6-1 - Initial HornetQ Donation Commit 2014-11-10 10:31:25 -06:00

readme.html

<html>
  <head>
    <title>HornetQ JMS Topic Selector Example 1</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>JMS Topic Selector Example 1</h1>

     <p>This example shows how messages can be consumed from a topic using Message Selectors.</p>
     <p>Consumers (or Subscribers) will only consume messages routed to a topic that match the provided selector</p>
     <p>Topics and selectors are a standard part of JMS, please consult the JMS 1.1 specification for full details.</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 = getContext();</code>
        </pre>

        <li>We look-up the JMS topic object from JNDI</li>
        <pre class="prettyprint">
           <code>Topic topic = (Topic) initialContext.lookup("/topic/exampleTopic");</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>Create one non-durable subscriber with a specific filter</li>
        <pre class="prettyprint">
         <code>MessageConsumer messageConsumer1 = session.createConsumer(topic, "someID=1", false);</code>
        </pre>
        
        <li>Create a second non-durable subscriber with a specific filter</li>
        <pre class="prettyprint">
         <code>MessageConsumer messageConsumer2 = session.createConsumer(topic, "someID=2", false);</code>
        </pre>
        
        <li>Create a third non-durable subscriber without any filters, and it should receive the complete set of messages</li>
        <pre class="prettyprint">
         <code>MessageConsumer messageConsumer3 = session.createConsumer(topic, "someID=2", false);</code>
        </pre>

        <li>Send 20 messages, 10 with someID=1, 10 with someID=2 </li>
        <pre class="prettyprint"><code>
         for (int i = 1; i < 10; i++)
         {
            for (int someID = 1; someID <= 2; someID++)
            {
               TextMessage message1 = session.createTextMessage("This is a text message " + i +
                                                                " sent for someID=" +
                                                                someID);

               message1.setIntProperty("someID", someID);

               producer.send(message1);

               System.out.println("Sent message: " + message1.getText());
            }
         }</code></pre>

        <li>We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started</li>
        <pre class="prettyprint">           <code>connection.start();</code>
        </pre>


        <li>Consume the messages from MessageConsumer1, filtering out someID=2</li>
        <pre class="prettyprint"><code>
         for (;;)
         {
            TextMessage messageReceivedA = (TextMessage)messageConsumer1.receive(1000);
            if (messageReceivedA == null)
            {
               break;
            }

            System.out.println("messageConsumer1 received " + messageReceivedA.getText() +
                               " someID = " +
                               messageReceivedA.getIntProperty("someID"));
         }</code></pre>
           
        <li>Consume the messages from MessageConsumer2, filtering out someID=1</li>
        <pre class="prettyprint"><code>
        for (;;)
         {
            TextMessage messageReceivedB = (TextMessage)messageConsumer2.receive(1000);
            if (messageReceivedB == null)
            {
               break;
            }

            System.out.println("messageConsumer2 received " + messageReceivedB.getText() +
                               " someID = " +
                               messageReceivedB.getIntProperty("someID"));
         }</code></pre>

        <li>Consume the messages from MessageConsumer3, receiving the complete set of messages</li>
        <pre class="prettyprint"><code>
         for (;;)
         {
            TextMessage messageReceivedC = (TextMessage)messageConsumer3.receive(1000);
            if (messageReceivedC == null)
            {
               break;
            }
            System.out.println("messageConsumer3 received " + messageReceivedC.getText() +
                               " someID = " +
                               messageReceivedC.getIntProperty("someID"));
         }</code></pre>

        <li>Close the consumers</li>
        <pre class="prettyprint"><code>subscriberA.close();</code></pre>
        <pre class="prettyprint"><code>subscriberB.close();</code></pre>
        
        <li>Delete the subscriptions when you're done</li>
        <pre class="prettyprint"><code>session.unsubscribe("sub-a1");</code></pre>
		<pre class="prettyprint"><code>session.unsubscribe("sub-a2");</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>
  </body>
</html>