JMS Durable Subscription Example

This example demonstrates how to use a durable subscription with ActiveMQ.

Durable subscriptions are a standard part of JMS, please consult the JMS 1.1 specification for full details.

Unlike non durable subscriptions, the key function of durable subscriptions is that the messages contained in them persist longer than the lifetime of the subscriber - i.e. they will accumulate messages sent to the topic even if the subscriber is not currently connected. They will also survive server restarts. Note that for the messages to be persisted, the messages sent to them must be marked as persistent messages.

Example step-by-step

To run the example, simply type mvn verify from this directory

  1. 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 client-jndi.properties file in the directory ../common/config
  2.            initialContext = getContext();
            
  3. We look-up the JMS topic object from JNDI
  4.            Topic topic = (Topic) initialContext.lookup("/topic/exampleTopic");
            
  5. We look-up the JMS connection factory object from JNDI
  6.            ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
            
  7. We create a JMS connection
  8.            connection = cf.createConnection();
            
  9. We set the client-id on the connection. This must be the first operation performed on the connection object. The combination of client-id and durable subscription name uniquely identifies the durable subscription. Maybe different durable subscritions can have the same name if they belong to different client-id values
  10.            connection.setClientID("durable-client");
            
  11. We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started
  12.            connection.start();
            
  13. We create a JMS session. The session is created as non transacted and will auto acknowledge messages.
  14.            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
  15. We create a JMS message producer on the session. This will be used to send the messages.
  16. 	   MessageProducer messageProducer = session.createProducer(topic);
            
  17. We create the durable subscriber on the topic, specifying it's name. Since this is the first time the subscriber is created and a subscription with that name and for this client-id does not already exist, then the underlying durable subscription will be created, and a subscriber will be created and returned for that subscription.
  18.            TopicSubscriber subscriber = session.createDurableSubscriber(topic, "subscriber-1");
            
  19. We create a JMS text message, message 1, that we are going to send. Note that it must be a persistent message in order to survive server restart.
  20.            TextMessage message1 = session.createTextMessage("This is a text message 1");
            
  21. We send message 1 to the topic
  22.            messageProducer.send(message1);
            
  23. The message arrives in the subscription, and we consume the message from the subscription.
  24.            TextMessage messageReceived = (TextMessage)subscriber.receive();
            
  25. We create and send another text message, message 2, to the same topic
  26.            TextMessage message2 = session.createTextMessage("This is a text message 2");
                  
               messageProducer.send(message2);
            
  27. Now we close the subscriber. Since the subscription is durable it will continue to survive even though there is no subscriber attached to it. At this point you could even stop and restart the server and the subscription would survive!
  28.            subscriber.close();
            
  29. We now create another durable subscriber, with the same name and same client-id on the same topic. Since the durable subscrition already exists, it will simply return a new subscriber consuming from the same durable subscription instance as before
  30.  
               subscriber = session.createDurableSubscriber(topic, "subscriber-1");
            
  31. We consume message 2 which was sent before the first subscriber was closed.
  32.            messageReceived = (TextMessage)subscriber.receive();
            
  33. We close the second subscriber.
  34.            subscriber.close();
            
  35. Now we delete the underlying durable subscription. This will delete any remaining unacknowledged messages in the subscription and a new subscriber will not be able to access them
  36.            session.unsubscribe("subscriber-1");
            
  37. And finally, always remember to close your JMS connections and resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
  38.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }