Managing Durable Topic Subscriptions
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 an application needs to be able to receive messages that are published even when the application is inactive.
Creating a durable topic subscriber is similar to creating a nondurable message consumer, but you must also provide a name that identifies the durable subscription, as in the following example:
// Create a durable subscriber, supplying a uniquely-identifying name TopicSubscriber sub = session.createDurableSubscriber( topic, "mySub1_0001" );
When creating a JMS subscription in tenant mode, the username, topic name and subscription ID should be set in the following manner:
username =username!tenantdomainÂ
Example: username =rohana!rohana.com;Â
topicName = tenantdomain/topicName;Â
Example: topicName = rohana.com/Kumara;Â
subscription Id=tenantdomain/subscription idÂ
Example: subscription Id=rohana.com/K1;
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:
// Unsubscribe the durable subscriber created above session.unsubscribe( "mySub1_0001" );
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.
For an example of how to use durable topic subscriptions, see Creating a Durable Topic Subscription.Â