Durable topics persist messages so that if a subscriber is not online, it can receive the messages later. A durable topic subscription is useful when a subscriber client needs to be able to receive messages that are published even when the client is inactive.
Creating a durable topic subscription is similar to creating a nondurable subscription, but you must additionally provide a name that identifies the durable subscription as shown below.
The session used to create a durable topic subscriber must have an associated client identifier. The client identifier can be specified by setting the CLIENTID property of the ConnectionFactory object. The client identifier associated with a session is the same as that associated with the connection that is used to create the session as shown below. The name that identifies a durable subscription must be unique only within the client identifier, and therefore the client identifier forms part of the full, unique identifier of the durable subscription.
For example, shown below is how the ClientID is set for an amqp session:
Alternatively, an application can specify the client identifier by calling the
setClientID()method of the Connection object. For more information about client identifiers and their relationship with durable topic subscribers and durable subscriptions, see Java™ Message Service Specification, Version 1.1.
Reusing a durable topic subscription
To reuse a durable subscription that was created previously, the subscriber client must create a durable topic subscriber using a session with the same client identifier as that associated with the durable subscription.
Tenant-specific durable subscriptions
When creating a JMS subscription in tenant mode, the username, topic name and subscription ID should be set in the following manner:
Example: username =testuser!test.com;
topicName = tenantdomain/topicName;
Example: topicName = test.com/testTopic;
subscription Id=tenantdomain/subscription id
Example: subscription Id=test.com/K1;
Managing nondurable topic subscriptions
Nondurable message consumers in the publish/subscribe domain automatically deregister themselves when their
close() method is called or when they fall out of scope. However, if you want to terminate a durable subscription, you must explicitly notify the broker. To do this, use the
unsubscribe() method of the session and pass in the name that identifies the durable subscription:
It is not possible to unsubscribe a durable consumer from inside the
onMessage() method in the listener. If you need to unsubscribe, write a separate class/thread etc. with a different session, give the subscription ID and unsubscribe.