# Topic Hierarchy Example

HornetQ supports topic hierarchies. With a topic hierarchy you can register a subscriber with a wild-card and that subscriber will receive any messages routed to an address that match the wildcard.

HornetQ wild-cards can use the character '#' which means "match any number of words", and the character '*' which means "match a single word". Words are delimited by the character "."

For example if I subscribe using the wild-card "news.europe.#", then that would match messages sent to the addresses "news.europe", "news.europe.sport" and "news.europe.entertainment", but it does not match messages sent to the address "news.usa.wrestling"

For more information on the wild-card syntax please consult the user manual.

## Example step-by-step

To run the example, simply type `./build.sh` (or `build.bat` on windows) from this directory

In this example we will define a hierarchy of topics in the file `hornetq-jms.xml`

       <topic name="news">
          <entry name="/topic/news"/>
       </topic>

       <topic name="news.usa">
          <entry name="/topic/news.usa"/>
       </topic>

       <topic name="news.usa.wrestling">
          <entry name="/topic/news.wrestling"/>
       </topic>

       <topic name="news.europe">
          <entry name="/topic/news.europe"/>
       </topic>

       <topic name="news.europe.sport">
          <entry name="/topic/news.europe.sport"/>
       </topic>

       <topic name="news.europe.entertainment">
          <entry name="/topic/news.europe.entertainment"/>
       </topic>

Then we will create a subscriber using the wildcard "news.europe.#".

We will then send three messages: one to the address news.usa.wrestling, one to the address news.europe.sport, and one to the address news.europe.entertainment.

We will verify that the message sent to news.usa.wrestling does not get received since it does not match, but the messages sent to the other two addresses do get received since they match.

1. Create an initial context to perform the JNDI lookup.

               initialContext = getContext(0);

2. Perform a lookup on the Connection Factory

               ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");

3. Create a JMS Connection

               connection = cf.createConnection();

4. Create a JMS Session

               Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

5. Instantiate a topic representing the wildcard we're going to subscribe to.

               Topic topicSubscribe = HornetQJMSClient.createHornetQTopic("news.europe.#");

6. Create a consumer (topic subscriber) that will consume using that wildcard. The consumer will receive any messages sent to any topic that starts with news.europe

              MessageConsumer messageConsumer = session.createConsumer(topicSubscribe);

7. Create an anonymous producer. The sending address is specified at send time.

               MessageProducer producer = session.createProducer(null);

8. Instantiate some more topic objects corresponding to the individual topics we're going to send messages to. You could look these up from JNDI if you wanted to.

             Topic topicNewsUsaWrestling = HornetQJMSClient.createHornetQTopic("news.usa.wrestling");

             Topic topicNewsEuropeSport = HornetQJMSClient.createHornetQTopic("news.europe.sport");

             Topic topicNewsEuropeEntertainment = HornetQJMSClient.createHornetQTopic("news.europe.entertainment");

9. Send a message destined for the usa wrestling topic.

             TextMessage messageWrestlingNews = session.createTextMessage("Hulk Hogan starts ballet classes");

             producer.send(topicNewsUsaWrestling, messageWrestlingNews);

10. Send a message destined for the europe sport topic.

             TextMessage messageEuropeSport = session.createTextMessage("Lewis Hamilton joins European synchronized swimming team");

             producer.send(topicNewsEuropeSport, messageEuropeSport);

11. Send a message destined for the europe entertainment topic

             TextMessage messageEuropeEntertainment = session.createTextMessage("John Lennon resurrected from dead");

             producer.send(topicNewsEuropeEntertainment, messageEuropeEntertainment);

12. Start the connection

            connection.start();

13. We don't receive the usa wrestling message since we subscribed to news.europe.# and that doesn't match news.usa.wrestling. However we do receive the Europe sport message, and the europe entertainment message, since these match the wildcard.

            TextMessage messageReceived1 = (TextMessage)messageConsumer.receive(5000);

            System.out.println("Received message: " + messageReceived1.getText());

            TextMessage messageReceived2 = (TextMessage)messageConsumer.receive(5000);

            System.out.println("Received message: " + messageReceived2.getText());

            Message message = messageConsumer.receive(1000);

            if (message != null)
            {
               return false;
            }

            System.out.println("Didn't received any more message: " + message);

14. Be sure to close our resources!

               finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }