/java/

ActiveMQ – Java – P&S - Durable v Nondurable subscribers

2016-07-19 22:53:46

This example is almost the same as here activemq-jms-first-touch

and base on it we can learn something new, and it is difference between Durable and Nondurable Subscriber.

To test how Nondurable Subscriber works, simpli check this example activemq-jms-first-touch.

Test scenario 1:

  1. Start Publisher java app
  2. Start Subscriber java app
  3. Type some messages in Publisher
  4. Check this message on Subscriber
  5. You should see the messages
  6. Stop Subscriber
  7. Type some messages in Publisher
  8. Start Subscriber
  9. You will see noting, no old message will be revise, and that is ok for current connection type.

This is how Nondurable Subscriber works.

To switch Subscriber to *Durable *mode you have to:

set client id for connection (TopicConnection)

...
topicConnection.setClientID("1");
...

and instead of creating subscriberSession.createSubscriber use subscriberSession.createDurableSubscriber

...
TopicSubscriber subscriber = subscriberSession.createDurableSubscriber(chatTopic, "Subscriber1");
...

Test scenario 2:

  1. Start Publisher java app
  2. Start Subscriber java app
  3. Type some messages in Publisher
  4. Check this message on Subscriber
  5. You should see the messages
  6. Stop Subscriber
  7. Type some messages in Publisher
  8. Start Subscriber
  9. You should see the messages.

As you see the main difference is that Durable subscriber has Cache functionality that store messages for Subscriber when it is not active.