Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Table of Contents
maxLevel4
minLevel3

Creating durable topic subscriptions

If a client subscriber creates a durable subscription to the topic, the subscriber will be able to recover the messages that were published to the topic while the client was inactive. For example, a durable topic subscriber will receive all the messages that are published to the topic while the subscriber is active. However, if the subscriber becomes inactive for a time period and later returns to active state, the messages that were published during the inactive period will be fetched from the database and dispatched to the subscriber. 

...

  • 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.

    Code Block
    languagejava
    // Create a durable subscriber, supplying a uniquely-identifying name
    TopicSubscriber sub = session.createDurableSubscriber( topic, "mySub1_0001" );
  • 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: Shown below is the connection URL used for subscribing to WSO2 MB during an AMQP session. See the topic on setting the connection URL for more information.

    Code Block
    connectionfactory.Connectionfactory = amqp://admin:admin@clientID/carbon?brokerlist='tcp://192.168.10.5:5682'
    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.  

...

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.

Sharing a durable topic subscription

As explained in the previous section, durable topic subscriptions are unique. That is, the subscription ID and client ID, which are used for identifying the durable subscription should be unique. This means that it is not possible to have multiple subscriber clients sharing a single subscription. Now lets let's look at what is meant by 'sharing' a durable subscription and how this possibility can be enabled for WSO2 MB.

Understanding shared durable topic subscriptions

Consider the following scenario:

...

As shown above, what we need is to establish a single subscription where the messages received are shared by multiple subscriber clients using the round-robin method. In the default set up of WSO2 MB, this is not possible, because when there are multiple subscriber clients connecting to the topic, copies of the same messages are dispatched to all the subscriber clients. Therefore, sharing a subscription is only possible if the subscription ID can be shared by all the subscriber clients.

Shared durable subscriptions in a clustered setup

Now, lets look at how this functionality of sharing a durable topic subscription works when the brokering facility is distributed among multiple MB nodes. Consider the following scenario:
 

...

Note

Note that if you unsubscribe one shared topic subscriber, it will affect all other durable topic subscriptions with the same subscription ID.

Enabling shared durable topic subscription in WSO2 MB

You can enable shared topic subscriptions in WSO2 MB by following the steps given below.

  1. Open the broker.xml file stored in the <MB_HOME>/repository/conf folder.
  2. Enable the allowSharedTopicSubscriptions element as shown below. Note that this property is only applicable to AMQP.

    Code Block
    <broker>
     <transports>
      <amqp enabled="true">
       <allowSharedTopicSubscriptions>true</allowSharedTopicSubscriptions>
      </amqp>
     </transports>
    </broker>
  3. The JMS clients that are subscribing to the topic should create durable subscriptions using the same subscription ID. See the previous section on creating durable topics subscriptions.