activemq-artemis/examples/jms/topic-selector-example1
Martyn Taylor 56d6a47a83 ActiveMQ6-65 JBoss JMS1.1 -> Geronimo 2.0 spec jar
Replaces usage of the JBoss 1.1 API jar with the Geronimo JMS 2.0 jar.
The API is backwards compatibile.
2015-01-07 19:54:42 +00:00
..
src/main ACTIVEMQ6-43(reopened) : Replace License Headers on codebase 2015-01-05 13:14:25 -05:00
pom.xml ActiveMQ6-65 JBoss JMS1.1 -> Geronimo 2.0 spec jar 2015-01-07 19:54:42 +00:00
readme.html ACTIVEMQ6-43(reopened) : Replace License Headers on codebase 2015-01-05 13:14:25 -05:00

readme.html

<!--
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 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>