Contact Us 1-800-596-4880

Using Topic Subscriptions

Topics in JMS allow you to configure how the subscription behaves regarding the connection of the consumer and also how this consumers interact between each other.

Using Mule’s JMS Connector you can handle of the aspects of a Topic subscription using the 'topic-consumer' configuration in both the consumer and listener operations.

Registering Durable Subscriptions

Durable subscriptions allows you to receive messages that were published to the topic while the consumer was offline. Once a durable subscription registered, each time a consumer is available it will receive all the messages published before the connection was established:

<jms:consume config-ref="JMS_config" destination="#[vars.destination]">
    <jms:consumer-type>
        <jms:topic-consumer durable="true" subscriptionName="SampleSub"/>
    </jms:consumer-type>
</jms:consume>

Then the JMS provider stores the messages sent or published to the topic, as it would store messages sent to a queue. If this or another application creates a 'topic-consumer' using the same connection and its client ID, the same topic, and the same subscription name, the subscription is reactivated, and the JMS provider delivers the messages that were published while the subscriber was inactive.

Allowing Multiple Consumers on the Same Subscription

In JMS 1.1, a subscription on a topic was not permitted to have more than one consumer at a time, meaning that the work of processing messages on a topic subscription could not be shared, thereby limiting the scalability of the application. This restriction has been removed in JMS 2.0 by the introduction of shared subscriptions.

The JMS Connector, when configured with a JMS 2.0 connection, allows you to declare any 'topic-consumer' as part of a shared subscription:

<jms:listener config-ref="config" destination="InvoiceEventsTopic">
    <jms:consumer-type>
        <jms:topic-consumer shared="true" subscriptionName="ClusterSubscription"/>
    </jms:consumer-type>
</jms:listener>

With this configuration on two different listener instances, when a Message is published to the invoices destination it will be delivered only to one of the listeners and not the other.

How to Ignore Self Published Messages

There are cases in which you may not want to receive the Messages that were published to a Topic by the same application. For this cases, you can use the noLocal parameter in the 'topic-consumer', thus preventing the Consumer to receive Messages published to the Topic by the same Connection:

<jms:listener config-ref="config" destination="${notificationsChannel}">
    <jms:consumer-type>
        <jms:topic-consumer noLocal="true"/>
    </jms:consumer-type>
</jms:listener>

Consumers configured as noLocal can’t be part of a shared subscription.

View on GitHub