# JMS Clustered Topic Example

This example demonstrates a JMS Topic deployed on two different nodes. The two nodes are configured to form a cluster.

We then create a subscriber on the topic on each node, and we create a producer on only one of the nodes.

We then send some messages via the producer, and we verify that both subscribers receive all the sent messages.

A JMS Topic is an example of publish-subscribe messaging where all subscribers receive all the messages sent to the topic (assuming they have no message selectors).

This example uses JNDI to lookup the JMS Queue and ConnectionFactory objects. If you prefer not to use JNDI, these could be instantiated directly.

Here's the relevant snippet from the server configuration, which tells the server to form a cluster between the two nodes and to load balance the messages between the nodes.

         <cluster-connection name="my-cluster">
            <address>jms</address>
            <retry-interval>500</retry-interval>
            <use-duplicate-detection>true</use-duplicate-detection>
            <forward-when-no-consumers>true</forward-when-no-consumers>
            <max-hops>1</max-hops>
            <discovery-group-ref discovery-group-name="my-discovery-group"/>
         </cluster-connection>

For more information on HornetQ load balancing, and clustering in general, please see the clustering section of the user manual.

## Example step-by-step

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

1. Get an initial context for looking up JNDI from server 0.

       ic0 = getContext(0);

2. Look-up the JMS Topic object from JNDI

               Topic topic = (Topic)ic0.lookup("/topic/exampleTopic");

3. Look-up a JMS Connection Factory object from JNDI on server 0

               ConnectionFactory cf0 = (ConnectionFactory)ic0.lookup("/ConnectionFactory");

4. Get an initial context for looking up JNDI from server 1.

               ic1 = getContext(1);

5. Look-up a JMS Connection Factory object from JNDI on server 1

               ConnectionFactory cf1 = (ConnectionFactory)ic1.lookup("/ConnectionFactory");

6. We create a JMS Connection connection0 which is a connection to server 0

       connection0 = cf0.createConnection();

7. We create a JMS Connection connection1 which is a connection to server 1

       connection1 = cf1.createConnection();

8. We create a JMS Session on server 0

       Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);

9. We create a JMS Session on server 1

       Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);

10. We start the connections to ensure delivery occurs on them

       connection0.start();

       connection1.start();

11. We create JMS MessageConsumer (Topic subscriber) objects on server 0 and server 1

       MessageConsumer consumer0 = session0.createConsumer(topic);

       MessageConsumer consumer1 = session1.createConsumer(topic);

12. We create a JMS MessageProducer object on server 0.

       MessageProducer producer = session0.createProducer(topic);

13. We send some messages to server 0.

    	final int numMessages = 10;

    	for (int i = 0; i < numMessages; i++)
    	{
    	   TextMessage message = session0.createTextMessage("This is text message " + i);

    	   producer.send(message);

    	   System.out.println("Sent message: " + message.getText());
    	}

14. We now consume those messages on both server 0 and server 1. We note that all messages have been consumed by both consumers. JMS Topics implement publish-subscribe messaging where all consumers get a copy of all messages.

        	for (int i = 0; i < numMessages; i ++)
        	{
        	   TextMessage message0 = (TextMessage)consumer0.receive(5000);

        	   System.out.println("Got message: " + message0.getText() + " from node 0");

        	   TextMessage message1 = (TextMessage)consumer1.receive(5000);

        	   System.out.println("Got message: " + message1.getText() + " from node 1");
        	}

15. And finally (no pun intended), always remember to close your JMS resources after use, in a `finally` block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects

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

    	   if (connection1 != null)
    	   {
    	      connection1.close();
    	   }
    	}