activemq-artemis/examples/jms/topic-selector-example1
jbertram a102983d7a ACTIVEMQ6-51 Example server bootstrapping 2014-12-10 09:49:13 -06:00
..
src/main ACTIVEMQ6-51 Example server bootstrapping 2014-12-10 09:49:13 -06:00
pom.xml ACTIVEMQ6-51 Example server bootstrapping 2014-12-10 09:49:13 -06:00
readme.html Remove references to HornetQ in doc and Comments 2014-11-19 15:15:35 +00:00

readme.html

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