I will tell you here what are
Durable and where those two words apply because these two words sometimes cause confusion to users of JMS providers. Generally,
Persistent applies to messages while
Durable applies to subscriptions.
A message can have either one of the delivery modes – Persistent and Non-persistent – when the message is sent by its originating client.
According to JMS specification when a message is sent with delivery mode as Persistent then JMS provider takes an extra care to ensure that message is not lost due to JMS provider failure. So in this scenario message will be saved on disk and sent later if consumer is inactive.
By default the delivery mode of a message is Persistent.
Now if you want to send the message with Non-persistent mode then you can specify the delivery mode at MessageProducer (QueueSender or TopicProducer) using the following snippets of code
Alternately, you can achieve the same thing using the following snippets of code
messageProducer.send(message, deliveryMode, priority, timeToLive);
For Non-persistent delivery mode the message will be saved in-memory and sent later if consumer is inactive but the message will not survive due to JMS provider failure.
Durable is applicable only for publish/subscribe domain. A subscription on a topic can either be durable or non-durable.
Let’s create an example on Non-durable subscriber
InitialContext initialContext = new InitialContext(); ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("jms/ConnectionFactory"); TopicConnection topicConnection = connectionFactory.createTopicConnection(); TopicSession topicSession = topicConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE); TopicSubscriber subscriber = topicSession.createSubscriber(topic); topicConnection.start(); Message message = subscriber.receive(5000); topicConnection.close()
Let’s create an example on Durable subscriber
InitialContext initialContext = new InitialContext(); ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("jms/ConnectionFactory"); TopicConnection topicConnection = connectionFactory.createTopicConnection(); TopicSession topicSession = topicConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE); TopicSubscriber subscriber = topicSession.createDurableSubscriber(topic, "durSub"); topicConnection.start(); Message message = subscriber.receive(5000); topicConnection.close()
So from the above two examples you see they are identical except in latter case we have changed to
createDurableSubscriber(Topic topic, String subName) to create a durable subscriber, where subName is a name used to identify the subscription.
receive(long timeout) method blocks until a message is received or until the timeout period expires, whichever comes first.
The non-durable and the durable subscribers they behave in much the same way. Whenever a new message is sent to the topic, a copy of that message is sent to both the non-durable and the durable subscribers. An open subscriber is said to be active.
The difference is when a non-durable subscriber is closed; it is considered to no longer exist and is deleted from the MQ broker. When a durable subscription is closed, however it continues to exist in the MQ broker, accumulating messages. A closed durable subscriber is said to be inactive.
Now consider the case when a subscriber is not available and message is sent to a topic.
If client connects subsequently and creates a non-durable subscriber; the subscriber will not receive the message that was sent prior to its existence.
However if client connects and creates a durable subscriber then the subscriber is re-opened and the message that was sent prior to its existence now will receive.
Note that durable and non-durable subscribers apply only to the topic because queue is having a single, built-in durable subscription shared by all consumers.
When a persistent message is sent to a topic, the MQ broker’s behavior depends on what subscriptions (if any) are present and in particular whether these subscriptions are durable or non-durable.
For durable subscriber, whether active or inactive, then a copy of the message will be saved on disk and a copy of each message will be sent to the active subscribers, but for inactive subscribers the message will remain saved on disk until they become active.
For non-durable subscribers a copy of each message will be dispatched to them while subscriber are active but the messages are not saved either in memory or disk. Therefore, a non-durable subscriber which connects after the message was sent will not receive the message.
Please have a look at the Durable Subscription
Thanks for reading.