activemq-artemis/examples/jms/topic-selector-example1
Martyn Taylor 3fb3799f91 [maven-release-plugin] prepare release 1.0.0 2015-05-20 11:17:31 +01:00
..
src/main fixed examples and some docs after Artemis renaming 2015-04-30 10:44:16 +01:00
pom.xml [maven-release-plugin] prepare release 1.0.0 2015-05-20 11:17:31 +01:00
readme.html Ensure all references to the project use ActiveMQ Artemis 2015-05-13 11:51:26 +01: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 Artemis 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 -Pexample</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>